SlideShare a Scribd company logo
1 of 13
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

More Related Content

What's hot

Inline function
Inline functionInline function
Inline function
Tech_MX
 

What's hot (20)

Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by Balaguruswami
 
Data types in C
Data types in CData types in C
Data types in C
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
 
Inline function
Inline functionInline function
Inline function
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 

Similar to Nesting of if else statement & Else If Ladder

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptx
Sasideepa
 

Similar to Nesting of if else statement & Else If Ladder (20)

C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Programming C Part 03
Programming C Part 03Programming C Part 03
Programming C Part 03
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptx
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 

More from Vishvesh Jasani (9)

Row space | Column Space | Null space | Rank | Nullity
Row space | Column Space | Null space | Rank | NullityRow space | Column Space | Null space | Rank | Nullity
Row space | Column Space | Null space | Rank | Nullity
 
Air Pollution Control act
Air Pollution Control actAir Pollution Control act
Air Pollution Control act
 
Types Of Reciprocating Member of the Pump
Types Of Reciprocating Member of the PumpTypes Of Reciprocating Member of the Pump
Types Of Reciprocating Member of the Pump
 
Projection Of Plane
Projection Of PlaneProjection Of Plane
Projection Of Plane
 
Summing Amplifer
Summing AmpliferSumming Amplifer
Summing Amplifer
 
Advantages of OPTICAL FIBER CABLE
Advantages of OPTICAL FIBER CABLEAdvantages of OPTICAL FIBER CABLE
Advantages of OPTICAL FIBER CABLE
 
Definitons-Electric Field,Lines of Force,Electric Intensity
Definitons-Electric Field,Lines of Force,Electric Intensity Definitons-Electric Field,Lines of Force,Electric Intensity
Definitons-Electric Field,Lines of Force,Electric Intensity
 
Difference between Hearing & Listening,Types of Listening ...
Difference between Hearing & Listening,Types of Listening                    ...Difference between Hearing & Listening,Types of Listening                    ...
Difference between Hearing & Listening,Types of Listening ...
 
Continuity of a Function
Continuity of a Function Continuity of a Function
Continuity of a Function
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

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 of presentation 1. Introduction 2. Simple if else statement 3. Nesting of if else statement 4. Else if Ladder
  • 3. 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);
  • 4. 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
  • 5. 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.
  • 6. 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
  • 7. 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); }
  • 8. // 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)>; } 9
  • 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; 10
  • 11. 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
  • 12. 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 ); }