Control Flow: Conditionals
Sue Inn Chng
The University of 1
Image credit: http://phdcomics.com/comics/archive_print.php?comicid=1987
Copyright By PowCoder代写 加微信 powcoder
Making Decisions in Life
– We make decisions every day.
– If happy, clap hands.
– If not happy, stomp feet.
– If I am thirsty and hungry, I will order a meal set.
– If it is sunny and I’m feeling well, take a walk in the park. Else, stay home.
– If I am tired or the time is between 10pm and 2am in the morning, I will sleep.
Else, if it is Monday 6pm-10pm, I will work on INFO1110 labs. Else, if it is Tuesday 6pm-10pm, I will go kickboxing.
On every other evening, I will farm on WoW with my mates!
– Do you notice anything? The University of 2
Programs with Decisions
– Instructcomputertoperformsetofactionsdependingon whether a condition is fulfilled.
– Selectionstatements:
– Conditional Execution – if
– AlternativeExecution–if-else – Chained conditionals – if-elif
The University of 3
Control Flow: if
– Syntax: – Flowchart
if
The University of 4
Recap: Boolean and Comparison Operators
– Boolean values: True, False
– A mixed type operator which takes operands of one type and returns of
any type (Boolean).
The University of 5
Recap: Boolean Operators
– Alsocalledlogicaloperators.
True if P is False. False if P is True
True if P and Q are True. Otherwise, False.
True if either P or Q is True. Otherwise, False.
– Truthtablepresentsresultofeverytruthcombinationgiven inputs.
The University of 6
Example from Week 3
– Canyouspottheconditionandaction?
if
– Can you determine the possible outcomes of __name__ == “__main__”?
The University of 7
– Thisisaspecialvariable.
– The__name__attributewillbesetto__main__ifthe
module is considered to be the main program.
– When a Python module or package is imported, __name__ is set to the module’s name. Usually, this is the name of the Python file itself without the .py extension
– https://docs.python.org/3/library/__main__.html The University of 8
Flowcharts
– Present plan of solution independent of programming language.
– We will use these to visualize the flow of execution.
Image credit: https://www.owlnet.rice.edu/~ceng303/manuals/fortran/FOR3_3.html
The University of 9
Example – Simple Conditions
– Write a program that prompts user for an integer and display ‘positive’ if number is more than 0.
– Ed Lesson The University of 10
Example: Complex Conditions
– What is the output of the program?
Conditions
x AND n<20
y AND n>15
AND (y AND n>15)
– Go to: Ed Lesson
The University of 11
Example: Nested if
Predict the value in variable ticket at the end of the program if speed is
initialize to: •5
Write a program to check your answer.
The University of 12
Stop: Check your understanding
– Review the questions: Ed Lesson
– Canyoudrawtheflowchartforeachquestion?
The University of 13
Control Flow: if
– Remember to initialize all
variables to a useful value.
– Safest to list out all possibilities
condition can take when using ifs.
– Not an issue, if it is a simple case. Being human, we can easily make a mistake and miss a case. 🙁
– pass statement is a null statement i.e. do nothing. Placeholder when statement is required syntactically but no codes needs to be executed.
The University of 14
Control Flow: if…else
– Syntax: – Flowchart
if
elseS:tatement 2
DO NOT put expressions after keyword else.
The University of 15
Example: Positive and Negative Numbers
– What happens if num > 0 is False?
– Implementtheflowchartand check the behaviour of your program for an appropriate value of num.
– Ed Lesson The University of 16
Example – Odd or Even
– Write a program that prompts user for an integer and display if it is odd or even.
– EdLesson
The University of 17
– Selection statements allows the flow of execution to follow a specific branch depending on the condition.
– Different types of branching:
if Nested ifs The University of Sydney
Acknowledgements
– The slides in this lecture are taken from materials developed by and the Sedgewick textbook.
The University of 19
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com