SlideShare a Scribd company logo
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 statements
Saad 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 control
ENGWAU TONNY
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
SHRIRANG PINJARKAR
 
Selection statements
Selection statementsSelection statements
Selection statements
Harsh Dabas
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
Praveen M Jigajinni
 
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
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Hossain Md Shakhawat
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
maznabili
 
If and select statement
If and select statementIf and select statement
If and select statement
Rahul Sharma
 
Control statements
Control statementsControl statements
Control statements
CutyChhaya
 
Decision statements
Decision statementsDecision statements
Decision statements
Jaya Kumari
 
03a control structures
03a   control structures03a   control structures
03a control structures
Manzoor ALam
 
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)
Papon Sarker
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test Bank
Lauriewest24
 
Python decision making part4
Python decision making part4Python decision making part4
Python decision making part4
Vishal Dutt
 
loops.pdf
loops.pdfloops.pdf
loops.pdf
AmayJaiswal4
 

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

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 

Recently uploaded (20)

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 

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