Java conditional statement if usage

Source: Internet
Author: User

1. if

 

Syntax:



Execution process:

 

 

For example:

 

 

Note: if there is only one execution statement when the if condition is set, you can omit braces! However, if multiple statements are executed, braces are indispensable ~~

2. if... Else

If... ElseThe operation of a statement is one step more than that of an if statement: when the condition is set, the code block of the if part is executed. if the condition is not set, the else part is entered. For example, if the exam score is greater than 90, an IPHONE 5S will be rewarded; otherwise, 500 push-ups will be fined.

 

Syntax:



Execution process:

 

 

For example:


3. Multiple if

 

Multiple if statementsWhen condition 1 is not met, condition 2 is judged. If none of the current conditions are met, the code in the else block is executed. For example, if the exam score is greater than 90, an IPHONE 5S will be rewarded. If the score is between 70 and 90, a red rice will be rewarded; otherwise, 500 push-ups will be fined.


Syntax:



Execution process:



For example:


 

The meaning of this section of code is: if the score value is greater than 90, then reward an Iphone 5S, when the score value is less than or equal to 90, first judge whether the score is greater than 70, if the score is between 70-90, a red rice is rewarded. If the score is smaller than or equal to 70, 500 push-ups are penalized.


When the condition to be determined is a continuous interval, it is very convenient to use multiple if statements.

4. Nested if

 

Nested if statement, The condition of the inner if is determined only when the condition of the outer if is set. For example, if today is a workday, you can go to work. If today is a weekend, you can go out to play. If the weather on the weekend is fine, you can go to an outdoor playground, otherwise, go to the indoor playground.

 

Syntax:

 

 

Execution process:

 

 

For example:

 

 

The running result is: Go to the outdoor playground

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.