SlideShare uma empresa Scribd logo
1 de 30
Baixar para ler 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

Mais conteúdo relacionado

Semelhante a W4-DecisionMaking.pdf. python document. CS

Semelhante a 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
 

Último

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
 
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
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
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
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
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
 
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
 
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
 

Último (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
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
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
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
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
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
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
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
 
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🔝
 
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
 
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
 

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