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