You can also extend an
if
instruction with an
else
statement. The
else
statement is carried out if and
only if the condition is not fulfilled.
// if instruction with else statement
if (Condition) {
/
/ The following is only carried out
/
/ if the condition is fulfilled.
Instruction 1;
I
nstruction 22;
...
} else {
/
/ This block is carried out if
/
/ the condition is not fulfilled.
I
nstruction 3;
Instruction 4;
}
Finally, you can build in the additional block
else if
to check another condition if the first one is not
fulfilled.
// if instruction with else block
if (Condition 1) {
/
/ The following is only carried out
/
/ if the condition is fulfilled.
Instruction 1;
I
nstruction 2;
...
} else if (Condition 2){
/
/ This block is carried out if
/
/ the condition is not fulfilled.
I
nstruction 3;
Instruction 4;
} else {
/
/ This block is carried out if
/
/ none of the previously checked
/
/ conditions are fulfilled.
I
nstruction 5;
...
}
You can also omit the closing
else
statement. In
addition, you can line up as many
else if
statements in a row as you like.
îî
CodeGamer
CodeGamer manual inside english.indd 25 7/19/16 12:32 PM