Else If Ladder (Format)
Else If Ladder (Format)
Else If Ladder (Format)
else if ladder
• When multipath decisions are involved, else if ladder is
used.
• The conditions are evaluated from the top of the ladder to
downwards.
• As soon as true condition is found, the statement
associated with it executed and the control is transferred to
the statement-x skipping rest of the ladder.
• When all n conditions become false, then the final else
containing the default statement will be executed.
Write a C program to find the grade according to obtained
marks according to the following table using else if ladder
Numerical grade Letter grade Grade point
80% or above A+ (A Plus) 4.0
75% to less than 80% A (A Regular) 3.75
70% to less than 75% A- (A Minus) 3.5
65% to less than 70% B+ (B Plus) 3.25
60% to less than 65% B (B Regular) 3.0
55% to less than 60% B- (B Minus) 2.75
50% to less than 55% C+ (C Plus) 2.5
45% to less than 50% C(C Regular) 2.25
40% to less than 45% D 2.0
Less than 40% F 0
Write a C program to find the attendance marks according to
percentage of days present using the following table using else if ladder
Attendance Marks
90% and above 10%
85% to less than 90% 9%
80% to less than 85% 8%
75% to less than 80% 7%
70% to less than 75% 6%
65% to less than 70% 5%
60% to less than 65% 4%
Less than 60% 0%
An electronic power distribution company charges its domestic
consumers as follows. Write a program that can read the customer
number and power consumed and prints the amount to be paid by
the customer.
Consumption Unit Rate of Charge
0-200 0.50 per unit
201-400 100 plus 0.65 per unit excess of 200
401-600 230 plus 0.80 per unit excess of 400
601 and above 390 plus 1.00 per unit excess of 600
Switch statement
• Complexity of a program increases dramatically when the
number of alternatives increases.
• The program becomes difficult to read and follow.
• At times, it may confuse even the person who designed it.
• In this cases, multiway decision statement switch is used.
• The switch statement tests the value of a given variable (or
expression) against a list of case values and when a match
is found, a block of statements associated with that case is
executed.
switch (expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
………………….
default:
default-block
break;
}
statement-x;
• The expression is an integer expression or characters.
• value-1, value-2……. are constants or constant expression and are known as
case lebels.
• Each of these values should be unique within a switch statement.
• block-1, block-2………. are statement lists and may contain zero or more
statements.
• Note that case lebels end with a colon (:)
• When the switch is executed, the value of the expression is successfully
compared against the values value-1,value-2….
• If a case is found whose value matches with the value of expression, then
the block of statements that follows the cases are executed.
• The break statement at the end of each block signals the end of a particular
case and causes an exit from the switch statement, transferring the control
to the statement-x following the switch.
Switch statement
The grading is done according to the following rules:
Average Marks Grade
80-100 Honors
60-79 First Division
50-59 Second Division
40-49 Third Division
0-39 Fail
Switch statement
Using a conversion statement: index= marks/10;
Marks Index
100 10
90-99 9
80-89 8
70-79 7
60-69 6
50-59 5
40-49 4
30-39 3
20-29 2
10-19 1
0 0