SlideShare a Scribd company logo
1 of 20
Download to read offline
Decision Control Statements
Conditional Steps
Output:
Smaller
End
Program:
x = 5
if x < 10:
print('Smaller')
if x > 20:
print('Bigger')
print(‘End')
x = 5
x < 10 ?
print('Smaller')
x > 20 ?
print('Bigger')
print(‘End')
Yes
No
Yes
No
Comparison Operators
• Boolean expressions ask a
question and produce a Yes or No
result which we use to control
program flow
• Boolean expressions using
comparison operators evaluate to
True / False or Yes / No
• Comparison operators look at
variables but do not change the
variables
Remember: “=” is used for assignment.
Python Meaning
< Less than
<= Less than or Equal to
== Equal to
>= Greater than or Equal to
> Greater than
!= Not equal
Comparison Operators
x = 5
if x == 5 :
print('Equals 5')
if x > 4 :
print('Greater than 4')
if x >= 5 :
print('Greater than or Equals 5')
if x < 6 : print('Less than 6')
if x <= 5 :
print('Less than or Equals 5')
if x != 6 :
print('Not equal 6')
Equals 5
Greater than 4
Greater than or Equals 5
Less than 6
Less than or Equals 5
Not equal 6
One-Way Decisions
x = 5
print('Before 5')
if x == 5 :
print('Is 5')
print('Is Still 5')
print('Third 5')
print('Afterwards 5')
print('Before 6')
if x == 6 :
print('Is 6')
print('Is Still 6')
print('Third 6')
print('Afterwards 6')
Before 5
Is 5
Is Still 5
Third 5
Afterwards 5
Before 6
Afterwards 6
x == 5 ?
Yes
print('Still 5')
print('Third 5')
No print('Is 5’)
Indentation
• Increase indent indent after an if statement or for statement (after : )
• Maintain indent to indicate the scope of the block (which lines are affected
by the if/for)
• Reduce indent back to the level of the if statement or for statement to
indicate the end of the block
• Blank lines are ignored - they do not affect indentation
• Comments on a line by themselves are ignored with regard to indentation
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')
for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
increase / maintain after if or for
decrease to indicate end of block
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')
for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
Think About begin/end Blocks
x = 42
if x > 1 :
print('More than one')
if x < 100 :
print('Less than 100')
print('All done')
Nested
Decisions
x > 1
print('More than one’)
x < 100
print('Less than 100')
print('All Done')
yes
yes
no
no
Two-way Decisions
• Sometimes we want to
do one thing if a logical
expression is true and
something else if the
expression is false
• It is like a fork in the
road - we must choose
one or the other path but
not both
x > 2
print('Bigger')
yes
no
x = 4
print('Not bigger')
print('All Done')
Two-way Decisions
with else:
x > 2
print('Bigger')
yes
no
x = 4
print('All Done')
x = 4
if x > 2 :
print('Bigger')
else :
print('Smaller')
print('All done')
print('Not bigger')
Visualize Blocks
x = 4
if x > 2 :
print('Bigger')
else :
print('Smaller')
print('All done')
x > 2
print('Bigger')
yes
no
x = 4
print('All Done')
print('Not bigger')
More Conditional Structures…
Multi-way
if x < 2 :
print('small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
x < 2 print('small')
yes
no
print('All Done')
x < 10 print('Medium')
yes
print('LARGE')
no
Multi-way
x = 0
if x < 2 :
print('small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
x < 2 print('small')
yes
no
print('All Done')
x < 10 print('Medium')
yes
print('LARGE')
no
x = 0
Multi-way
x = 5
if x < 2 :
print('small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
x < 2 print('small')
yes
no
print('All Done')
x < 10 print('Medium')
yes
print('LARGE')
no
x = 5
Multi-way
x = 20
if x < 2 :
print('small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
x < 2 print('small')
yes
no
print('All Done')
x < 10 print('Medium')
yes
print('LARGE')
no
x = 20
Multi-way
# No Else
x = 5
if x < 2 :
print('Small')
elif x < 10 :
print('Medium')
print('All done')
if x < 2 :
print('Small')
elif x < 10 :
print('Medium')
elif x < 20 :
print('Big')
elif x < 40 :
print('Large')
elif x < 100:
print('Huge')
else :
print('Ginormous')
Multi-way Puzzles
if x < 2 :
print('Below 2')
elif x < 20 :
print('Below 20')
elif x < 10 :
print('Below 10')
else :
print('Something else')
if x < 2 :
print('Below 2')
elif x >= 2 :
print('Two or more')
else :
print('Something else')
Which will never print
regardless of the value for x?
Summary
• Comparison operators
== <= >= > < !=
• Indentation
• One-way Decisions
• Two-way decisions: if: and else:
• Nested Decisions
• Multi-way decisions using elif

More Related Content

Similar to Decision and Control Statements.pdf

Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3Paul Brebner
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxssuser2f67c91
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Absolute Value
Absolute ValueAbsolute Value
Absolute Valuempscils598s07
 
The Ring programming language version 1.10 book - Part 27 of 212
The Ring programming language version 1.10 book - Part 27 of 212The Ring programming language version 1.10 book - Part 27 of 212
The Ring programming language version 1.10 book - Part 27 of 212Mahmoud Samir Fayed
 
Absolute Value Notes
Absolute Value NotesAbsolute Value Notes
Absolute Value Notesmpscils598s07
 
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189Mahmoud Samir Fayed
 
Python for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.comPython for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.comShwetaAggarwal56
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAKTabsheer Hasan
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdfRohitSindhu10
 
Python Control structures
Python Control structuresPython Control structures
Python Control structuresSiddique Ibrahim
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxSALU18
 
Pengenalan kepada pengaturcaraan berstruktur
Pengenalan kepada pengaturcaraan berstrukturPengenalan kepada pengaturcaraan berstruktur
Pengenalan kepada pengaturcaraan berstrukturUnit Kediaman Luar Kampus
 

Similar to Decision and Control Statements.pdf (20)

R part I
R part IR part I
R part I
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptx
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Absolute Value
Absolute ValueAbsolute Value
Absolute Value
 
The Ring programming language version 1.10 book - Part 27 of 212
The Ring programming language version 1.10 book - Part 27 of 212The Ring programming language version 1.10 book - Part 27 of 212
The Ring programming language version 1.10 book - Part 27 of 212
 
Absolute Value Notes
Absolute Value NotesAbsolute Value Notes
Absolute Value Notes
 
Operators
OperatorsOperators
Operators
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
conditional.ppt
conditional.pptconditional.ppt
conditional.ppt
 
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189
 
Python for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.comPython for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.com
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAK
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Pengenalan kepada pengaturcaraan berstruktur
Pengenalan kepada pengaturcaraan berstrukturPengenalan kepada pengaturcaraan berstruktur
Pengenalan kepada pengaturcaraan berstruktur
 

Recently uploaded

TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfAnubhavMangla3
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Hiroshi SHIBATA
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data SciencePaolo Missier
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxMasterG
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?Paolo Missier
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPTiSEO AI
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 

Recently uploaded (20)

TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 

Decision and Control Statements.pdf

  • 2. Conditional Steps Output: Smaller End Program: x = 5 if x < 10: print('Smaller') if x > 20: print('Bigger') print(‘End') x = 5 x < 10 ? print('Smaller') x > 20 ? print('Bigger') print(‘End') Yes No Yes No
  • 3. Comparison Operators • Boolean expressions ask a question and produce a Yes or No result which we use to control program flow • Boolean expressions using comparison operators evaluate to True / False or Yes / No • Comparison operators look at variables but do not change the variables Remember: “=” is used for assignment. Python Meaning < Less than <= Less than or Equal to == Equal to >= Greater than or Equal to > Greater than != Not equal
  • 4. Comparison Operators x = 5 if x == 5 : print('Equals 5') if x > 4 : print('Greater than 4') if x >= 5 : print('Greater than or Equals 5') if x < 6 : print('Less than 6') if x <= 5 : print('Less than or Equals 5') if x != 6 : print('Not equal 6') Equals 5 Greater than 4 Greater than or Equals 5 Less than 6 Less than or Equals 5 Not equal 6
  • 5. One-Way Decisions x = 5 print('Before 5') if x == 5 : print('Is 5') print('Is Still 5') print('Third 5') print('Afterwards 5') print('Before 6') if x == 6 : print('Is 6') print('Is Still 6') print('Third 6') print('Afterwards 6') Before 5 Is 5 Is Still 5 Third 5 Afterwards 5 Before 6 Afterwards 6 x == 5 ? Yes print('Still 5') print('Third 5') No print('Is 5’)
  • 6. Indentation • Increase indent indent after an if statement or for statement (after : ) • Maintain indent to indicate the scope of the block (which lines are affected by the if/for) • Reduce indent back to the level of the if statement or for statement to indicate the end of the block • Blank lines are ignored - they do not affect indentation • Comments on a line by themselves are ignored with regard to indentation
  • 7. x = 5 if x > 2 : print('Bigger than 2') print('Still bigger') print('Done with 2') for i in range(5) : print(i) if i > 2 : print('Bigger than 2') print('Done with i', i) print('All Done') increase / maintain after if or for decrease to indicate end of block
  • 8. x = 5 if x > 2 : print('Bigger than 2') print('Still bigger') print('Done with 2') for i in range(5) : print(i) if i > 2 : print('Bigger than 2') print('Done with i', i) print('All Done') Think About begin/end Blocks
  • 9. x = 42 if x > 1 : print('More than one') if x < 100 : print('Less than 100') print('All done') Nested Decisions x > 1 print('More than one’) x < 100 print('Less than 100') print('All Done') yes yes no no
  • 10. Two-way Decisions • Sometimes we want to do one thing if a logical expression is true and something else if the expression is false • It is like a fork in the road - we must choose one or the other path but not both x > 2 print('Bigger') yes no x = 4 print('Not bigger') print('All Done')
  • 11. Two-way Decisions with else: x > 2 print('Bigger') yes no x = 4 print('All Done') x = 4 if x > 2 : print('Bigger') else : print('Smaller') print('All done') print('Not bigger')
  • 12. Visualize Blocks x = 4 if x > 2 : print('Bigger') else : print('Smaller') print('All done') x > 2 print('Bigger') yes no x = 4 print('All Done') print('Not bigger')
  • 14. Multi-way if x < 2 : print('small') elif x < 10 : print('Medium') else : print('LARGE') print('All done') x < 2 print('small') yes no print('All Done') x < 10 print('Medium') yes print('LARGE') no
  • 15. Multi-way x = 0 if x < 2 : print('small') elif x < 10 : print('Medium') else : print('LARGE') print('All done') x < 2 print('small') yes no print('All Done') x < 10 print('Medium') yes print('LARGE') no x = 0
  • 16. Multi-way x = 5 if x < 2 : print('small') elif x < 10 : print('Medium') else : print('LARGE') print('All done') x < 2 print('small') yes no print('All Done') x < 10 print('Medium') yes print('LARGE') no x = 5
  • 17. Multi-way x = 20 if x < 2 : print('small') elif x < 10 : print('Medium') else : print('LARGE') print('All done') x < 2 print('small') yes no print('All Done') x < 10 print('Medium') yes print('LARGE') no x = 20
  • 18. Multi-way # No Else x = 5 if x < 2 : print('Small') elif x < 10 : print('Medium') print('All done') if x < 2 : print('Small') elif x < 10 : print('Medium') elif x < 20 : print('Big') elif x < 40 : print('Large') elif x < 100: print('Huge') else : print('Ginormous')
  • 19. Multi-way Puzzles if x < 2 : print('Below 2') elif x < 20 : print('Below 20') elif x < 10 : print('Below 10') else : print('Something else') if x < 2 : print('Below 2') elif x >= 2 : print('Two or more') else : print('Something else') Which will never print regardless of the value for x?
  • 20. Summary • Comparison operators == <= >= > < != • Indentation • One-way Decisions • Two-way decisions: if: and else: • Nested Decisions • Multi-way decisions using elif