SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie W4-DecisionMaking.pdf. python document. CS

Ähnlich wie 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
 

Kürzlich hochgeladen

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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
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
 
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
 
_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
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 

Kürzlich hochgeladen (20)

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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
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
 
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
 
_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
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 

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