This document summarizes control flow statements in Python including sequential execution, conditional execution with if/else statements, and repetitive execution with while and for loops. It provides examples of if, elif, nested if, while, and for statements. It also discusses breaking and continuing loops using break and continue statements.
2. Control Flow Statements/Structure
▸ Python supports a set of control flow statements that you can integrate
into your program
▸ Sequential
▸ Line by line from top to bottom execution of statement in the same order they
appear in the program
▸ Decision
▸ Allows conditionally execute or not execute a block of code.
▸ Loop
▸ Allow the execution of a block of statement multiple time.
Condition/if Statement
Repetition/ Iteration Statement
3. If Control Flow Statement
if boolean_expression :
statement-1
statement-2
statement-3
else :
statement-4
keyword
block of Code
colon
keyword
another
block
9. ▸ We can as many as elif we want.
If Control Flow Statement
if boolean_expression-1 :
statement-1
statement-2
elif boolean_expression-2 :
statement-3
statement-4
elif boolean_expression-3 :
statement-5
else :
statement-6
keyword
13. Nested if Statements
▸ We can place an if statement inside another if statement.
if boolean_expression-1 :
if boolean_expression-2
statement-1
else
statement-2
elif boolean_expression-3 :
statement-3
else :
statement-4
will match
the closest if
22. For Control Flow Statement
for iteration_variable in sequence:
statement-1
statement-2
statement-3
Block of Code
keyword Colon
sequence
of items
keyworditem
23. range is a built-
in function
remove end=‘, ‘
and see what will
happen
24. super easy program but the
details are always important
you can write
this program in 100
ways
here we did
not check user
input. but we have
to!
25. Break and Continue
▸ If you wand to loop until something occurs, but you’re not sure when
that might happen, you can use an infinite loop with a break statement.
▸ If you don’t want to break out of a loop but just want to skip ahead to
the next iteration for some reason you can use continue statement.