SlideShare a Scribd company logo
1 of 6
3.2. Conditional (if), alternative (if-else), chained conditional (if-elif-else)
CONTROL STRUCTURES
if statement
if-else statement
if-elif-else statement
Nested Conditional
while loop
for loop
nested loop
break statement
pass statement
continue statement
Decision Making (or) Conditionals (or) Branching
The execution of the program depends upon the condition. The sequence of the control
flow differs from the normal program. The decision making statements evaluate the conditions
and produce True or False as outcome.
Types of conditional Statement
1. if statement
2. if-else statement
3. if-elif-else statement
4. Nested Conditional
Decision Making Iteration Unconditional Statement
Control structures
1.if statement(conditional)
If statement contains a logical expression using which data is compared and a decision is
made based on the result of comparison.
Syntax
Flow Chart
False
Example:
a=10
if a==10:
print(“a is equal to 10”)
Result:
a is equal to 10
Test
Expres
Body of if stmt
True
if <expression>:
true statements
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
True
Test
Expres
False
2. Alternative execution (if… else)
The second form of if statement is “alternative execution” in which there are two possibilities
and the condition determines which block of statement executes.
Syntax
If the testexpression evaluates to true then <body_1>statements are executed else
<body_2> statements are executed.
Flow Chart
Example:
a=10
if a==10:
print(“a is equal to 10”)
else:
print(“a is not equal to 10”)
Result
a is equal to 10
if <testexpression>:
<body_1>
else:
Body of else
Body of if
End of Code
3.elif else Statement(chained conditionals)
The elif statement or chained conditional allows you to check multiple expressions for
true and execute a block of code as soon as one of the conditions evaluates to true. The elif
statement has more than one statements and there is only one if condition.
Body of if
Test
Expressi
on
False
Syntax
False
Flow Chart
Test
Expressi
on
if <test_expression_1>:
<body1>
elif <test_expression_2>:
<body2>
elif <test_expression_3>:
<body3>
True
True
Body of elif Body of else
End of Code
Example:
a=9
if a==10:
print(“a is equal to 10”)
elif a<10:
print(“a is lesser than 10”)
elif a>10:
print(“a is greater than 10”)
else
Result:
print(“a is not equal to 10”)
a is lesser than 10
4. Nested Conditionals (or) Nested if-else
One conditional can also be nested with another condition.(ie) we can have if…elif….else
statement inside another if …elif…else statements.
Syntax
if expression 1:
true statements
else:
if expression 2:
true statements
else:
false statement
Flow Chart
True
Test
Conditio
n 1
False
True
Test
Conditio
n 2
False
End of Code
Body of else
Body of if
Body of if
Example:
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
else:
else:
print("Zero")
print("Positive number")
print("Negative number")
Result:
Enter a number:50
Positive number

More Related Content

Similar to conditional statements.docx

Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlENGWAU TONNY
 
Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
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_selectionalish 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_selectionalish sha
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statementsmaznabili
 
If and select statement
If and select statementIf and select statement
If and select statementRahul Sharma
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Decision statements
Decision statementsDecision statements
Decision statementsJaya Kumari
 
03a control structures
03a   control structures03a   control structures
03a control structuresManzoor ALam
 
Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)Papon Sarker
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankLauriewest24
 
Python decision making part4
Python decision making part4Python decision making part4
Python decision making part4Vishal Dutt
 

Similar to conditional statements.docx (20)

Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
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
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
If and select statement
If and select statementIf and select statement
If and select statement
 
Control statements
Control statementsControl statements
Control statements
 
Decision statements
Decision statementsDecision statements
Decision statements
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test Bank
 
Python decision making part4
Python decision making part4Python decision making part4
Python decision making part4
 
loops.pdf
loops.pdfloops.pdf
loops.pdf
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

conditional statements.docx

  • 1. 3.2. Conditional (if), alternative (if-else), chained conditional (if-elif-else) CONTROL STRUCTURES if statement if-else statement if-elif-else statement Nested Conditional while loop for loop nested loop break statement pass statement continue statement Decision Making (or) Conditionals (or) Branching The execution of the program depends upon the condition. The sequence of the control flow differs from the normal program. The decision making statements evaluate the conditions and produce True or False as outcome. Types of conditional Statement 1. if statement 2. if-else statement 3. if-elif-else statement 4. Nested Conditional Decision Making Iteration Unconditional Statement Control structures
  • 2. 1.if statement(conditional) If statement contains a logical expression using which data is compared and a decision is made based on the result of comparison. Syntax Flow Chart False Example: a=10 if a==10: print(“a is equal to 10”) Result: a is equal to 10 Test Expres Body of if stmt True if <expression>: true statements
  • 3. ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY True Test Expres False 2. Alternative execution (if… else) The second form of if statement is “alternative execution” in which there are two possibilities and the condition determines which block of statement executes. Syntax If the testexpression evaluates to true then <body_1>statements are executed else <body_2> statements are executed. Flow Chart Example: a=10 if a==10: print(“a is equal to 10”) else: print(“a is not equal to 10”) Result a is equal to 10 if <testexpression>: <body_1> else: Body of else Body of if End of Code
  • 4. 3.elif else Statement(chained conditionals) The elif statement or chained conditional allows you to check multiple expressions for true and execute a block of code as soon as one of the conditions evaluates to true. The elif statement has more than one statements and there is only one if condition. Body of if Test Expressi on False Syntax False Flow Chart Test Expressi on if <test_expression_1>: <body1> elif <test_expression_2>: <body2> elif <test_expression_3>: <body3> True True Body of elif Body of else End of Code
  • 5. Example: a=9 if a==10: print(“a is equal to 10”) elif a<10: print(“a is lesser than 10”) elif a>10: print(“a is greater than 10”) else Result: print(“a is not equal to 10”) a is lesser than 10 4. Nested Conditionals (or) Nested if-else One conditional can also be nested with another condition.(ie) we can have if…elif….else statement inside another if …elif…else statements. Syntax if expression 1: true statements else: if expression 2: true statements else: false statement
  • 6. Flow Chart True Test Conditio n 1 False True Test Conditio n 2 False End of Code Body of else Body of if Body of if Example: num = float(input("Enter a number: ")) if num >= 0: if num == 0: else: else: print("Zero") print("Positive number") print("Negative number") Result: Enter a number:50 Positive number