More Control: Selection/
Conditional
Conditional statement
1. if booleanExpression:
Task1
2. if booleanExpression:
Task1
else:
Task2
3. if booleanExpression:
Task1
elif booleanExpression:
Task2
else:
Task3
General format, suites
Continue statement
• A continue statement, if executed in a loop, means to immediately
jump back to the top of the loop and re-evaluate the conditional
• Any remaining parts of the loop are skipped for the one iteration
when the continue was exectued
Break statement
• A break statement in a loop, if executed, exits the loop
• It exists immediately, skipping whatever remains of the loop as well as
the else statement (if it exists) of the loop
Putting all the variations of while into one generic pattern
while boolean expression1:
# statement suite1
if boolean expression2:
break # Exit loop now; skip else.
if boolean expression3:
continue # Go to top of loop now.
if boolean expression 4:
# do sth here
# rest of the code
6
More Control: Repetition
while loop, round two
• while loop, oddly, can have an associated else suite
• else suite is executed when the loop finishes under normal
conditions
• basically the last thing the loop does as it exits
while with else
while booleanExpression:
Task 1
else:
Task 2
rest of the program
More on the for Statement
Simple for to find the sum of the numbers from 1 to 100
for statement support the control modifiers continue, else and break.
Structure:
for target in object:
# statement suite1
if boolean expression1:
break # Exit loop now; skip else.
if boolean expression2:
continue # Go to top of loop now.
else:
# statement suite2
11
Decide which to use, for or while?
• The while loop is more general but the for loop is useful for moving
through all the elements of a collection.
• Fortunately, many objects in Python can be examined using iteration.
• Think for first!
12
Nesting
Inserting the suite of any control construct (if, while, for) within another
control construct (if, while, for)
While …
for …
if …
while …
else
13
Program to Illustrate the Use of Continue in While
Consider writing a program that continuously prompts a user for a series of even
integers that the program will sum together. If the user makes an error and enters a
non-even (odd) number, the program should indicate an error, ignore that input,
and continue with the process. Given this process, we need a way to end the loop.
Let’s choose a special character to stop the looping. In our case, if the special
character “.” is entered, the program will print the final sum and end.
Algorithm:
1. Prompt the user for a number.
2. Convert the input string to an int.
3. If the input is even, add it to the running sum.
4. If the input is not even (odd), print an error message—don’t add it into the sum, just
continue on.
5. If the input is the special character “.”, end and print the final sum.
N.B. The solution is given in later part of the slides
14
Practice Runs
Task 1 (if/elif/else): Grade message
Question:
Write a program that takes an integer score (0–100).
•If score ≥ 90 → print "A"
•Else if score ≥ 80 → print "B"
•Else if score ≥ 70 → print "C"
Practice Runs
Task 1 (if/elif/else): Grade message
Question:
Write a program that takes an integer score (0–100).
•If score ≥ 90 → print "A"
•Else if score ≥ 80 → print "B"
•Else if score ≥ 70 → print "C"
Practice Runs
Task 2 (AND / OR): Free shipping
Question:
Input total (bill amount) and is_member (yes/no).
Print "Free shipping" if total >= 50 OR the user is a member. Otherwise print "No free shipping".
Practice Runs
Task 2 (AND / OR): Free shipping
Question:
Input total (bill amount) and is_member (yes/no).
Print "Free shipping" if total >= 50 OR the user is a member. Otherwise print "No free shipping".
Practice Runs
Task 3 (for loop): Sum 1 to N
Question:
Input N. Print the sum of numbers from 1 to N. (Example: N=5 → 15)
Practice Runs
Task 3 (for loop): Sum 1 to N
Question:
Input N. Print the sum of numbers from 1 to N. (Example: N=5 → 15)
Practice Runs
Task 5 (while loop): Keep asking until correct password
Question:
Keep asking the user for a password until they type "python". Then print
"Access granted".
Practice Runs
Task 5 (while loop): Keep asking until correct password
Question:
Keep asking the user for a password until they type "python". Then print
"Access granted".