Coupon Accepted Successfully!


The else-if Ladder

A common programming construct is the else-if ladder, sometimes called the if-else-if staircase because of its appearance. In the program one can write a ladder of else-if. The program goes down the ladder of else-if, in anticipation of one of the expressions being true.

The general syntax of else-if ladder is as shown in Figure.

The else-if ladder

The conditions are evaluated from top to bottom. As soon as a true condition is met, the associated statement block gets executed and the rest of the ladder is bypassed. If none of the conditions are met, then the final else block is executed. If this else is not present and none of the if statements evaluate to true, then the entire ladder is bypassed

Programs on else if ladder are explained as follows.

5.9 Write a program to calculate energy bill. Read the starting and ending meter reading. The charges are as follows.


Explanation: Previous and current electrical readings are entered through the keyboard. Their difference is the total energy consumed. As per the table, rates are applied and the total bill based on consumption of energy is calculated.

5.10 Write a program to calculate gross salary for the conditions given below.


Basic Salary (Rs.) DA(Rs.) HRA(Rs.) Conveyance(Rs.)
bs>=10000 110% of Basic 20% of Basic 500
bs>=5000 && bs<=1000 100% of Basic 15% of Basic 400
bs<5000 90% of Basic 10% of Basic 300

Explanation: In the above program, the basic salary of an employee is entered through the keyboard. This entered figure is checked with different conditions as cited in the problem. The else-if statements are used. Based on the conditions, gross salary is calculated and displayed.

5.11 Write a program to simulate tariff charges for reaching different destinations by bus.


Explanation: As can be seen, the program simulates a bus station. We use an if-else-if ladder to find out the cost of a ticket to a particular station, if there was a bus to that station. There was no bus to that station that had cost=0. Finally, if the cost is non-zero, it is printed. The user is prompted to enter the choice. Depending upon the choice, the fare of destination station is displayed. In the above example, the choice 4 is given. The result displayed is


β€œThe ticket cost is : Rs 600


Pay the amount to book the ticket.”

Test Your Skills Now!
Take a Quiz now
Reviewer Name