java中的if-then和if-then-else条件语句

这个...

这个

Woman sitting at desk working on laptop writing Java conditional statements if-thenand if-then-elseconditional statements let a Java program make simple decisions

For 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.

The if-then Statement 

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 true

The syntax follows the same pattern every time. The

if if (condition is true) execute this statement

The key thing to remember is the condition must equate to a

boolean

Often, 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-then

The if-then-else Statement

The

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 false

For 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 > 35

However, 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)

  • 发表于 2021-10-02 14:56
  • 阅读 ( 197 )
  • 分类:编程

你可能感兴趣的文章

打破(break)和在java中继续(continue in java)的区别

关键区别——java中的break与continue 在编程中,有时需要多次重复一个语句或一组语句。循环用于对同一组指令进行多次迭代。循环的一些例子是while循环、do while循环和for循环。在while循环中,首先计算测试表达式。如果为true...

  • 发布于 2020-10-19 05:44
  • 阅读 ( 210 )

最终决赛(final finally)和用java完成(finalize in java)的区别

关键区别–java中的final vs finally vs finalize final、finally和finalize是Java编程中常用的术语。期末考试是一个关键词。它可以用于变量、方法或类。声明为final的变量只能初始化一次。他们不能改变。由于Java是一种支持面向对象编...

  • 发布于 2020-10-19 15:24
  • 阅读 ( 237 )

如果(if)和如果还有(if else)的区别

...f都包含要计算的表达式。在if中,如果条件为true,则if块中的语句将执行,并且控制被传递到if块后面的下一个语句。在if-else中,如果条件为true,则if块中的语句将执行;如果条件为false,则if-else块中的语句将执行。这就是if和i...

  • 发布于 2020-10-24 02:33
  • 阅读 ( 195 )

关于在excel中编写vba宏的初学者教程(以及学习的原因)

... if-then-else语句 ...

  • 发布于 2021-03-24 10:07
  • 阅读 ( 240 )

如何在microsoftexcel中使用if语句

... 使用if-then逻辑很强大 ...

  • 发布于 2021-03-26 12:25
  • 阅读 ( 204 )

如何用java编写for循环

... java中的for循环 ...

  • 发布于 2021-03-29 11:52
  • 阅读 ( 229 )

shell脚本编写初学者指南4:条件&if-then语句

... 就这么简单! 我现在该怎么办? 既然您知道如何使用“if-then-else”语句,就可以运行可以执行测试的脚本了。例如,您可以运行一个脚本来计算文件的md5哈希值,然后将其与您在文件中下载的哈希值进行比较,以查看它们是否...

  • 发布于 2021-04-12 20:28
  • 阅读 ( 181 )

用if-then语句减少不停的来回电子邮件

...自己经常沉迷于冗长的乒乓式电子邮件对话中,那么使用If-then语句可能会帮助你节省一些时间。把电子邮件当成聊天客户端会占用大量的时间,很快就会填满你的收件箱。如果你想减少来回的次数,亚洲效率公司的迈克·施密茨...

  • 发布于 2021-05-17 00:46
  • 阅读 ( 98 )

把你的目标变成if-then语句来解释障碍

...车需要修理。为了在挫折面前保持领先,给你的目标一个if-then计划。我们以前讨论过这个概念,是关于建立意志力的。要将你大脑中冲动的部分与平静、有条不紊的部分联系起来,用if-then语句来控制你的行为。例如,你可能会...

  • 发布于 2021-05-17 23:52
  • 阅读 ( 85 )

什么是伪码(what is the pseudocode)和算法?(algorithm?)的区别

...码时,有一些表示算法逻辑的标准术语,例如SEQUENCE,而IF-THEN-ELSE和其他结构也很有用,包括REPEAT-UNTIL、CASE和FOR。 这些术语被开发人员理解,并且有助于构建需求,而不使用其他人可能不理解的俚语或个人术语。 SEQUENCE表示从...

  • 发布于 2021-06-24 23:47
  • 阅读 ( 1607 )
wp7vvyua0pd
wp7vvyua0pd

0 篇文章

相关推荐