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

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
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
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
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
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
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
 
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
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
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
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
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
 
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
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

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