Friday 30 August 2013

The “else – if” Ladder | C Programming Language Tutorial pdf



The “else – if” Ladder:

=> This is another way of putting if „s together when multiple decisions are involved.
=> A multipath decision is a chain of if ?s in which the statement associated with each else is an if.
=> Hence it forms a ladder called else–if ladder.
Syntax:
if (Test Condition -1)
Statement -1;
else if ( Test Condition -2)
Statement -2;
else if ( Test Condition -3)
Statement -3;
:
:
:
:
else if ( Test Condition –n)
Statement –n;
else
default statement;
Rest of the Program Statements-X;

=> The above construction is known as else if ladders.
=> The conditions are evaluated from top to bottom.
=> As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the Rest of the Program Statement–X (skipping rest of the ladder).
=> When all the 'n' conditions become false, then the final else containing the default statement will be executed.
Flow Chart:

Image: 10

Program:
Write a program to read three numbers and find the largest one by using “else-if” ladder.
# include<stdio.h>
# include<conio.h>
main( )
{
int a, b, c
clrscr ( ) ;
printf(“Enter 1st number:”);
scanf(“%d”, &a);
printf(“Enter 2nd number:”);
scanf(“%d”, &b);
printf(“Enter 3rd number:”);
scanf(“%d”, &c);
if ((a>b) && (a>c))
printf(“Highest Number is: %d”, a);
else if ((b>a) && (b>c))
printf(“Highest Number is: %d”, b);
else
printf(“Highest Numbers is: %d”, c);
getch( );
}

Output:
Run-1:
Enter 1st number: 52
Enter 2nd number: 74
Enter 3rd number: 90
Highest Numbers is: 90
Run-2:
Enter 1st number: 81
Enter 2nd number: 237
Enter 3rd number: 65
Highest Numbers is: 237

1 comments:

ccodelearner on 17 September 2023 at 11:00 said...

To carry out actions based on a given circumstance, an if-else statement in C is utilized. This Scaler Topics article explains how to implement the decision-making process in C using if-else statements. C if else statement - An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved