KEMBAR78
Nesting of if else statement & Else If Ladder | PPTX
Gandhinagar Institute
of Technology(012)
Subject : CPU (2110003)
Active Learning Assignment
Branch : Computer
DIV. : A-2
Prepared by : - Vishvesh jasani (160120107042)
Guided By: Prof. Nirav Pandya
topic:Nesting of if else statement & Else If Ladder
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:
Enter an integer: 7
7 is an odd integer
Output
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
Nested if else if Flow Chart
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
}
9
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;
10
The else if ladder
falsetrue
true false
false
false
test
condition1
statement-nn default
statement
statement-1
statement-2
true
true
Entry
statement-3
statement-x
test
condition1
test
condition3
test
condition-n
11
Example
main()
{
int units, custnum;
float charges;
printf("Enter customer no. and units consumedn");
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("nnCustomer no:%d Charge=%.2fn", custnum, charges );
}
Nesting of if else statement & Else If Ladder

Nesting of if else statement & Else If Ladder

  • 1.
    Gandhinagar Institute of Technology(012) Subject: CPU (2110003) Active Learning Assignment Branch : Computer DIV. : A-2 Prepared by : - Vishvesh jasani (160120107042) Guided By: Prof. Nirav Pandya topic:Nesting of if else statement & Else If Ladder
  • 2.
    Brief flow ofpresentation 1. Introduction 2. Simple if else statement 3. Nesting of if else statement 4. Else if Ladder
  • 3.
    The if elseStatement 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);
  • 4.
    Flow chart // Trueif 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: Enter an integer: 7 7 is an odd integer Output
  • 5.
    Nested if...else statement -Theif...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.
  • 6.
    Syntax of nestedif...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
  • 7.
    Example #include <stdio.h> int main() { intnumber1, 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); }
  • 8.
    // if bothtest expression is false else { printf("Result: %d < %d",number1, number2); } return 0; } Output: Enter two integers: 12 23 Result: 12 < 23
  • 9.
    Nested if elseif Flow Chart if (<test>) { <statement(s)>; } else if (<test>) { <statement(s)>; } else if (<test>) { <statement(s)>; } 9
  • 10.
    The else ifladder 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; 10
  • 11.
    The else ifladder falsetrue true false false false test condition1 statement-nn default statement statement-1 statement-2 true true Entry statement-3 statement-x test condition1 test condition3 test condition-n 11
  • 12.
    Example main() { int units, custnum; floatcharges; printf("Enter customer no. and units consumedn"); 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("nnCustomer no:%d Charge=%.2fn", custnum, charges ); }