The most often used flow control statement is if statement, or extended if...else if...else.

This kind of statement controls the flow of the code execution based on the results of given comparison, as shown here:

if (expression) {

   statament1;

}

else {

   statement2;

}

The expression is converted into a Boolean (true or false) value and comparison is executed.

If the expression above evaluates to true (i.e. 5 > 2), then the statement1 will get executed; otherwise statement2 gets executed.

The if...else statement may be expandeded into if...else if...else statement, as shown here:

if (expression) {

   statament1;

}

else if {

   statement2;

}

else {

   statement3;

}

The else if extension is not limited to only one else if but in case of needing more than a couple, it is advised to use switch statement instead, which is more suitable for multiple flow detours.

Example

The example with if statement in JavaScript:

 

›› go to examples ››