About the If Statement
The if statement, which is used to conditionally execute a statement or block of statements, has three basic forms. The first form (if) is used to execute a statement if an expression is true, otherwise, do nothing. The second form (if/else) is used to execute one of two separate statements depending on whether an expression is true or not. The final form (if/else if/else) essentially chains together a sequence of conditionals to determine which statement from a collection of statements should be executed.
| Conditional | Statement |
|---|---|
| if | The first type of if statement has the format ``` if( expression ) |
statement For example, if(x>0) |
|
y=sqrt(x); This statement first evaluates the expression x>0. If this expression is **true**, the statement y=sqrt(x); is executed. If the expression, x>0 is **false**, the **if** statement does nothing. Note that the statement y=sqrt(x); ends with a semicolon. All statements must be terminated with a semicolon unless the statement is a statement block as in the following example: if(x>0) { |
|
| y=sqrt(x); | |
| z=x/(1+x); | |
| } ``` Each statement in a statement block ends with a semicolon. However, the block itself does not include any terminating punctuation other than the closing brace. Wherever you can insert a statement, you can instead insert a statement block. | |
| if/else | The second type of if statement has the format ``` if( expression ) |
| statement1 | |
| else | |
statement2 For example, if(x>0) |
|
| y=sqrt(x); | |
| else | |
y=0; The first part of this statement behaves just like the previous example. That is, the statement first evaluates the expression x>0. If this expression is **true**, the statement y=sqrt(x); is executed. However, if the expression is **false**, the statement y=0; is executed. Just as with the previous form of the **if** statement, each conditional statement can be a statement block. For example, if(x>0) { |
|
| y=sqrt(x); | |
| z=x/(1+x); | |
| } | |
| else { | |
| y=0; | |
| z=0; | |
| } ``` | |
| if/else if/else | The final type of if statement has the format ``` if( expression1 ) |
| statement1 | |
| else if( expression2 ) | |
| statement2 | |
| . . . | |
| else | |
statement3 There can be any number of else if parts in the full statement. Also, the final else is optional. For example, if(x>0) |
|
| y=sqrt(x); | |
| else if(x==0) | |
| y=-1; | |
| else | |
| y=0; ``` This statement first evaluates the expression x>0. If it is true, the statement y=sqrt(x); is executed. If the expression is false, the expression x==0 is evaluated. If this expression is true, then the statement y=-1; is executed. Finally, if none of the conditional expressions is true, the final statement, y=0;, is executed. | |
| IF Primitive | You can create a conditional using the IF primitive instead of the if statement. The statement ``` if( expression ) |
statement is the same as IF( expression, statement ) Also, the statement if( expression ) |
|
| statement1 | |
| else | |
statement2 is the same as IF( expression, statement1, statement2 ) Note that the word *IF* is in capital letters when using the **IF** primitive and it is in lower-case characters when using the **if** statement. For example, the statement if(x>0) |
|
| y=sqrt(x); | |
| else | |
y=0; is the same as IF(x>0,y=sqrt(x),y=0); Using the **IF** primitive, you can further simplify this expression by bringing the y= out in front of **IF**: y=IF(x>0,sqrt(x),0); ``` |