Conditional Statements

If…elseif…else statement

The if…elseif…else statement executes different codes for more than two conditions.

Syntax

if (condition 1) {
  // code to be executed if condition 1 is true
} elseif (condition 2) {
  // code to be executed if condition 1 is false and condition 2 is true
} else {
  // code to be executed if all conditions are false
}

Switch

switch statement selects one of many blocks of code to be executed.

switch (n) {
  case label1:
    // code to be executed if n=label1
    break;
  case label2:
    // code to be executed if n=label2
    break;
  case label3:
    // code to be executed if n=label3
    break;
    ...
  default:
    // code to be executed if n is different from all labels
}