If else if ladder



When a series of many conditions have to be checked we may use the ladder else if statement which takes the following general form.

Syntax
if (condition1)
   statement – 1;
else if (condition2)
   statement2;
else if (condition3)
   statement3;
else if (condition n)
   statement n;
else
   default statement;
   Statement-x;

  • This construct is known as if else construct or ladder.
  • The conditions are evaluated from the top of the ladder to downwards.
  • As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement – x (skipping the rest of the ladder).
  • When all the condition becomes false, the final else block containing the default statement will be executed.

Flow chart of if-else if ladder

if else if ladder