else if ladder in C

Nested if else technique to solve the problem in less effort and efficient manner

It is also another way of making decisions where multiple conditions are involved. In else if ladder statement there is one if condition, multiple else if conditions and one else block.

Let's see the syntax of else if ladder statement for better understanding, let's see the flow of else if ladder conditions.

Syntax

Working

Step 1

  • First, if condition will be checked.
  • If the if condition is true then if block will be executed and other remaining below conditions will be skipped.
  • If the if condition is false then the Step 2 will be follow and if block will not run.

Step 2

  • If step 1 is returns false then only step 2 will be executed.
  • Now the else if condition which is just after if condition will be cheked.
  • If the 1st else if condition is true then the scope of 1st else if condition will be executed and remaining below conditions will be skipped.
  • if 1st else if conditon is false then next else if condition will be check.
  • This process will continue until any true else if condition found.
  • If any else if condition found true then the scope of that else if block will run and rest conditions will be skipped.
  • If no else if condition is found true then step 3 will be continue.

Step 3

  • If the if condition and all else if conditions are false then only else block will be executed.

Example 1

Write a program that takes a number between 1 to 9 as input, Program prints input number in words as output.

Output

Example 2

Write a C program that takes exam mark as input and for output, if mark>=60 then print First Division, if mark>=40 then print Second Division, if mark>=30 then print Third Division and if mark<30 then print Fail.

Output

Points to remember

  • All conditions are checked from stating from if condition to else condition in sequence.
  • After true condition, remaining all conditions in else if ladder statements are skipped.
  • If no condition is true then else block is executed.
  • In programming use of else if ladder statement is better than the use of multiple if statements.

Nested conditions in else if ladder statement

According to the requirement of the program, we can use nested conditions within any block at any label. See the below example.

Output

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)