这个
if-thenand if-then-elseconditional statements let a Java program make simple decisionsFor example, when making a plan with a friend, you could say "If Mike gets home before 5:00 PM, then we'll go out for an early dinner." When 5:00 PM arrives, the condition (i.e., Mike is home), which determines whether everyone goes out for an early dinner, will either be true or false. It works exactly the same in Java.
Let's say part of a program we're writing needs to calculate if the purchaser of a ticket is eligible for a child's discount. Anyone under the age of 16 gets a 10% discount on the ticket price.
We can let our program make this decision by using an
if-then if (age < 16) isChild = true;In our program, an integer variable called
ageholds the age of the ticket purchaser. The condition (i.e., is the ticket purchaser under 16) is placed inside the brackets. If this condition is true, then the statement beneath the if statement is executed -- in this case a booleanvariable isChildis set to trueThe syntax follows the same pattern every time. The
if if (condition is true) execute this statementThe key thing to remember is the condition must equate to a
booleanOften, a Java program needs to execute more than one statement if a condition is true. This is achieved by using a block (i.e., enclosing the statements in curly brackets):
if (age < 16){ isChild = true; discount = 10;}This form of the
if-thenThe
if-thenstatement can be extended to have statements that are executed when the condition is false. The if-then-else if (condition) { execute statement(s) if condition is true}else{ execute statement(s) if condition is false}In the ticket program, let's say we need to make sure the discount is equal to 0 if the ticket purchaser is not a child:
if (age < 16){ isChild = true; discount = 10;}else{ discount = 0;}The
if-then-elsestatement also allows the nesting of if-then if (age < 16){ isChild = true; discount = 10;}else if (age > 65){ isPensioner = true; discount = 15;}else if (isStudent == true){ discount = 5;}As you can see, the
if-then-elsestatement pattern just repeats itself. If at any time the condition is true , then the relevant statements are executed and any conditions beneath are not tested to see whether they are trueor falseFor example, if the age of the ticket purchaser is 67, then the highlighted statements are executed and the
(isStudent == true)There is something worth noting about the
(isStudent == true)condition. The condition is written to make it clear that we're testing whether isStudenthas a value of true, but because it is a boolean else if (isStudent){ discount = 5;}If this is confusing, the way to think about it is like this -- we know a condition is tested to be true or false. For integer variables like
age, we have to write an expression that can be evaluated to true or false (e.g., age == 12, age > 35However, boolean variables already evaluate to be true or false. We don't need to write an expression to prove it because
if (isStudent)is already saying "if isStudent is true..". If you want to test that a boolean variable is false, just use the unary operator !. It inverts a boolean value, therefore if (!isStudent)关键区别——java中的break与continue 在编程中,有时需要多次重复一个语句或一组语句。循环用于对同一组指令进行多次迭代。循环的一些例子是while循环、do while循环和for循环。在while循环中,首先计算测试表达式。如果为true...
关键区别–java中的final vs finally vs finalize final、finally和finalize是Java编程中常用的术语。期末考试是一个关键词。它可以用于变量、方法或类。声明为final的变量只能初始化一次。他们不能改变。由于Java是一种支持面向对象编...
...f都包含要计算的表达式。在if中,如果条件为true,则if块中的语句将执行,并且控制被传递到if块后面的下一个语句。在if-else中,如果条件为true,则if块中的语句将执行;如果条件为false,则if-else块中的语句将执行。这就是if和i...
... 就这么简单! 我现在该怎么办? 既然您知道如何使用“if-then-else”语句,就可以运行可以执行测试的脚本了。例如,您可以运行一个脚本来计算文件的md5哈希值,然后将其与您在文件中下载的哈希值进行比较,以查看它们是否...
...自己经常沉迷于冗长的乒乓式电子邮件对话中,那么使用If-then语句可能会帮助你节省一些时间。把电子邮件当成聊天客户端会占用大量的时间,很快就会填满你的收件箱。如果你想减少来回的次数,亚洲效率公司的迈克·施密茨...
...车需要修理。为了在挫折面前保持领先,给你的目标一个if-then计划。我们以前讨论过这个概念,是关于建立意志力的。要将你大脑中冲动的部分与平静、有条不紊的部分联系起来,用if-then语句来控制你的行为。例如,你可能会...
...码时,有一些表示算法逻辑的标准术语,例如SEQUENCE,而IF-THEN-ELSE和其他结构也很有用,包括REPEAT-UNTIL、CASE和FOR。 这些术语被开发人员理解,并且有助于构建需求,而不使用其他人可能不理解的俚语或个人术语。 SEQUENCE表示从...