|
Previous Next
if Statement
The "if" is a keyword that lets you create a conditional expression. The condition that you are testing for is placed in parentheses after the "if" keyword. The statement or block of statements that get executed will follow the "if" statement. Optionally, you can include any number of "else if" to add more conditions if the first one is not true. It will continue to test the "else if" until one of the conditions is true. It will process the statement following that true condition then the testing will end. It will exit the "if-else" construction and executes what followings it, as follows.
if (this is true)
do this;
else if (this one is true)
do this one;
else
default to doing this;
now do this code after the testing;
If there are multiple statements to be executed when a condition is true, then they must be placed within brackets { }.
if (this is true) {
execute this step 1;
execute this step 2;
execute this step 3;
} else if (this one is true) {
execute this step A;
execute this step B;
execute this step C;
} else {
default to executing this step i;
default to executing this step ii;
default to executing this step iii;
}
Previous Next
|