Python Control Structure
By: Micheale Hadera
1
Python Control structure
2
Structured Programming
Structured programming is a programming paradigm aimed at
improving the clarity, quality, and development time of a
computer program by making extensive use of the structured
control flow constructs of selection (if/then/else) and repetition
(while and for), block structures, and subroutines.
The mechanisms that allow us to control the flow of execution are
called control structures.
3
Cont..
There are three main categories of control structures:
Sequence
Selection
Iteration
4
If-then-else
If (boolean condition) Then
(consequent)
Else
(alternative)
End If
5
Nested structure
if age is less than 18
you can't vote
if age is less than 16
you can't drive
else
you can drive
else
you can vote
if age is less than 21
you can't drink
else
6
you can drink
Conditionals
Python programming language assumes any non-zero and non-null values as
TRUE, and if it is either zero or null, then it is assumed as FALSE value.
If –else and elif
If statement statement Nested statement
if expression: if expression1:
if expression: statement(s) statement(s)
statement(s) elif expression: if expression2:
statement(s) statement(s)
else: elif expression3:
statement(s) statement(s)
elif expression4:
statement(s)
else:
statement(s)
else: 7
statement(s)
Conditionals
Example:
$python main.py
Expression value is less than 200
Which is 100
Good bye!
8
Iteration Control Structures
In iteration control structures, a statement or block is
executed until the program reaches a certain state, or
operations have been applied to every element of a
collection. This is usually expressed with keywords such
as while, repeat, for, or do..until.
9
Loops
While Loop
initialization of the flag
while the answer to the question is true then do
some statements or action
some statements or action
some statements or action
update the flag
10
Loops
The While Loop
$python main.py
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
11
Loops
For Loop
for
initialization of the starting value
starting value is less than the stopping value
some statements or action
some statements or action
some statements or action
increment the starting value
12
Loops-Branching statements
A branch is an instruction in a computer program that can cause a
computer to begin executing a different instruction sequence
and thus deviate from its default behavior of executing
instructions in order. Common branching statements include
break, continue, return, and goto.
13
Loops
The For Loop
$python main.py
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Current fruit : orange
Current fruit : papaya
Current fruit : beles
Good bye!
14
Loops
Loop control statements
❖ Break: Terminates the loop statement and transfer execution to the
statement immediately following the loop.
$python main.py
Current Letter : P
Current Letter : y
Current Letter : t
❖ Continue: causes the loop to skip the remainder of the body and immediately
retest its condition prior to iterating.
$python main.py
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n 15