Gandhinagar Institute
of Technology(012)
Active Learning Assignment
Subject : CPU (2110003)
topic:Nesting of if else statement & Else If Ladder
Branch : Computer
DIV. : A-2
Prepared by : - Vishvesh jasani (160120107042)
Guided By: Prof. Nirav Pandya
Brief flow of presentation
1. Introduction
2. Simple if else statement
3. Nesting of if else statement
4. Else if Ladder
The if else Statement
if else statement: A control structure that executes one block of
statements if a certain condition is true, and a second block of
statements if it is false. We refer to each block as a branch.
General syntax:
if (<test>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
Example:
#include<stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
Flow chart
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even
integer.",number);
else
printf("%d is an odd
integer.",number);
return 0;
Output
}
Output:
Enter an integer: 7
7 is an odd integer
Nested if...else statement
-The if...else statement executes two different codes depending
upon whether the test expression is true or false.
- Sometimes, a choice has to be made from more than 2
possibilities.
-The nested if...else statement allows you to check for multiple test
expressions and execute different codes for more than two
conditions.
Syntax of nested if...else statement
if (testExpression1)
{
// statements to be executed if testExpression1 is true
}
else if(testExpression2)
{
// statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3)
{
// statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is
true
}
.
else
{
// statements to be executed if all test expressions are false
Example
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if two integers are equal.
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}
// if both test expression is false
else
{
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output:
Enter two integers: 12
23
Result: 12 < 23
9
Nested if else if Flow Chart
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
}
10
The else if ladder
if ( test condition-1 )
statement-1
else if ( test condition-2 )
statement-2;
else if (condition-3)
statement-3;
else if ( condition-n)
statement-n;
else
default-statement;
statement-x;
11
The else if ladder
Entry
true test false
condition1
true test false
statement-1
condition1
true test false
statement-2 condition3
statement-3 true false
test
condition-n
statement-nn default
statement
statement-x
Example
main()
{
int units, custnum;
float charges;
printf("Enter customer no. and units consumed\n");
scanf("%d%d",&custnum,&units);
if(units<=200)
charges=0.5*units;
else if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.8*(units-400);
else
charges=390+(units-600);
printf("\n\nCustomer no:%d Charge=%.2f\n", custnum, charges );
}