SlideShare a Scribd company logo
1 of 30
Download to read offline
CS114 - Fundamental of Programming
Dr. Nazia Perwaiz
nazia.perwaiz@seecs.edu.pk
vision.seecs.edu.pk
Note: Various contents in this presentation have been taken from different books, lecture notes, and the web. These solely belong to their
owners, and are here used only for clarifying various educational concepts. Any copyright infringement is not intended.
2
DECISION MAKING (STRUCTURE)
Outline
• Python if else
• if statement
• if-else
• nested-if
• if-elif-else ladder
• Short Hand if statement
• Short Hand if-else statement
• Examples and practice
programmes
3
Decision-making
• There comes situations in real life when we need to make some decisions and
based on these decisions, we decide what should we do next.
• Similar situations arise in programming also where we need to make some
decisions and based on these decisions we will execute the next block of code.
• Decision-making statements in programming languages decide the direction of the
flow of program execution.
• In Python, if else elif statement is used for decision making
4
if statement (one way decision)
• ‘if’ statement is the most simple decision-making statement.
• It is used to decide whether a certain statement or block of statements will be
executed or not i.e if a certain condition is true then a block of statement is
executed otherwise not.
5
if statement
• Syntax:
• Here, the condition after evaluation will be either true or false. if statement accepts
boolean values – if the value is true then it will execute the block of statements
below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also.
6
if condition:
# Statements to execute if condition is true
if statement
7
As we know, python uses indentation to identify a block. So the block under an if statement will be
identified as shown in the below example
If statement flow chart
8
Example: Python if Statement
9
Example: Python if Statement
10
As the condition present in the if statement is false. So, the block below the if statement is not executed.
if-else (Two way decision)
• The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t.
• But what if we want to do something else if the condition is false. Here comes
the else statement.
• We can use the else statement with if statement to execute a block of code when
the condition is false.
11
12
FlowChart of Python if else statement
13
Example : Python if else statement
14
Example : Python if else statement
15
nested-if
• A nested if is an if statement that is the target of another if statement.
• Nested if statements mean an if statement inside another if statement.
• Python allows us to nest if statements within if statements. i.e, we can place an if
statement inside another if statement.
16
17
Flowchart of Python Nested if Statement
18
Example: Python Nested if
19
Example: Python Nested if
20
if-elif-else ladder (three way decision)
• A user can decide among multiple options.
• The if statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if is executed,
and the rest of the ladder is bypassed.
• If none of the conditions is true, then the final else statement will be executed.
21
if-elif-else ladder
22
FlowChart of Python if else elif statements
23
24
Example: Python if else elif statements
25
Short Hand if statement
26
Short Hand if-else statement
27
Short Hand if-else statement
28
i = 10
print(True) if i < 15 else print(False)
Short Hand if-else statement
29
i = 10
print(True) if i < 15 else print(False)
Output:
True
W4-DecisionMaking.pdf. python document. CS

More Related Content

Similar to W4-DecisionMaking.pdf. python document. CS

Similar to W4-DecisionMaking.pdf. python document. CS (20)

Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Control satkcher ppt
Control satkcher pptControl satkcher ppt
Control satkcher ppt
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Unit II.pptx
Unit II.pptxUnit II.pptx
Unit II.pptx
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Java Chapter 05 - Conditions & Loops: part 1
Java Chapter 05 - Conditions & Loops: part 1Java Chapter 05 - Conditions & Loops: part 1
Java Chapter 05 - Conditions & Loops: part 1
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
4. decision making and some basic problem
4. decision making and some basic problem4. decision making and some basic problem
4. decision making and some basic problem
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
 
All about decision making statements in php
All about decision making statements in phpAll about decision making statements in php
All about decision making statements in php
 
Decision structures chpt_5
Decision structures chpt_5Decision structures chpt_5
Decision structures chpt_5
 
com.pptx
com.pptxcom.pptx
com.pptx
 
Lect8
Lect8Lect8
Lect8
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
“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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
“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...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 

W4-DecisionMaking.pdf. python document. CS

  • 1. CS114 - Fundamental of Programming Dr. Nazia Perwaiz nazia.perwaiz@seecs.edu.pk vision.seecs.edu.pk Note: Various contents in this presentation have been taken from different books, lecture notes, and the web. These solely belong to their owners, and are here used only for clarifying various educational concepts. Any copyright infringement is not intended.
  • 3. Outline • Python if else • if statement • if-else • nested-if • if-elif-else ladder • Short Hand if statement • Short Hand if-else statement • Examples and practice programmes 3
  • 4. Decision-making • There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. • Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code. • Decision-making statements in programming languages decide the direction of the flow of program execution. • In Python, if else elif statement is used for decision making 4
  • 5. if statement (one way decision) • ‘if’ statement is the most simple decision-making statement. • It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. 5
  • 6. if statement • Syntax: • Here, the condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also. 6 if condition: # Statements to execute if condition is true
  • 7. if statement 7 As we know, python uses indentation to identify a block. So the block under an if statement will be identified as shown in the below example
  • 9. Example: Python if Statement 9
  • 10. Example: Python if Statement 10 As the condition present in the if statement is false. So, the block below the if statement is not executed.
  • 11. if-else (Two way decision) • The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. • But what if we want to do something else if the condition is false. Here comes the else statement. • We can use the else statement with if statement to execute a block of code when the condition is false. 11
  • 12. 12
  • 13. FlowChart of Python if else statement 13
  • 14. Example : Python if else statement 14
  • 15. Example : Python if else statement 15
  • 16. nested-if • A nested if is an if statement that is the target of another if statement. • Nested if statements mean an if statement inside another if statement. • Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement. 16
  • 17. 17
  • 18. Flowchart of Python Nested if Statement 18
  • 21. if-elif-else ladder (three way decision) • A user can decide among multiple options. • The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. • If none of the conditions is true, then the final else statement will be executed. 21
  • 23. FlowChart of Python if else elif statements 23
  • 24. 24
  • 25. Example: Python if else elif statements 25
  • 26. Short Hand if statement 26
  • 27. Short Hand if-else statement 27
  • 28. Short Hand if-else statement 28 i = 10 print(True) if i < 15 else print(False)
  • 29. Short Hand if-else statement 29 i = 10 print(True) if i < 15 else print(False) Output: True