[NAME] ALL.dao.control.if-else [TITLE] If-Else Conditional Control [DESCRIPTION] The if-else control allows the program to branch and execute different blocks of codes, based on the results of the condition expressions. 0.1 Definition 1 ControlBlock ::= Statement | '{' [ StatementBlock ] '}' 2 3 IfElseStmt ::= 'if' '(' [ LocalVarDeclaration ';' ] Expression ')' ControlBlock 4 { 'else' 'if' '(' [ LocalVarDeclaration ';' ] Expression ')' ControlBlock } 5 [ 'else' ControlBlock ] 0.2 Use Cases 0.2.1 Single Statement This is the simplest use case of if. It generally looks like the following: 1 if( condition ) statement Where the statement is executed only if the condition evaluated to true (non-zero number or enum value). 0.2.2 Single Block If multiple statements need to be executed if the if test is true, a pair of braces can b e used to group these statements into a block. Of course, such block can also nest other blocks. 1 if( condition ) { 2 block 3 } 0.2.3 Else Statement or Block If there is another statement or block should be executed when the condition test failed, one can add the else clause to the if statement. 1 if( condition ) 2 statement1 3 else 4 statement2 5 6 if( condition ) { 7 block1 8 } else { 9 block2 10 } Please note that, the braces are needed only if the block constains more than a single st atement. 0.2.4 Multiple Conditions and Blocks If there are multiple conditions to be tested for multiple statements or blocks, the else if clause can be used. 1 if( condition1 ){ 2 block1; 3 }else if( condition2 ){ 4 block2; 5 }else{ 6 block3; 7 } If condition1 is true, block1 is executed; otherwise, if condition2 is true, block2 is ex ecuted; otherwise, block3 is executed; zero or more else if and zero or one else statemen t can be used. 0.2.5 Optional Declaration Before each condition expression, there can be an optional variable declaration. 1 if( declaration; condition ) { 2 block 3 } For example, 1 if( a = sin(100); a > 0 ) io.writeln( "positive sine" ); 2 if( var a = sin(100); a > 0 ) io.writeln( "positive sine" ); 0.3 More Examples 1 var a = 5 2 if( a > 1 ) io.writeln( 'a > 1' ) 3 4 if( a > 2 ) 5 io.writeln( 'a > 2' ) 6 else 7 io.writeln( 'a <= 2' ) 8 9 if( a < 3 ) 10 io.writeln( 'a < 3' ) 11 else if( a < 4 ) 12 io.writeln( 'a < 4' ) 13 else 14 io.writeln( 'a >= 4' )