ADEL AHMED ALHAJJ
Reference
ADEL AHMED ALHAJJ
Control Structures(I)
Normally, statements are executed in the order in which they appear in the
program, and this is called sequential execution.
ADEL AHMED ALHAJJ
There are certain statements that cause execution to transfer to another
statement that may not be the next one in the program's sequence, and
this is called transfer of control.
Control structures in Python are divided into two categories:
3
Control Structures(I)
Conditional control structures (decision-making), which will be explained now.
ADEL AHMED ALHAJJ
Iterative control structures (loops), which will be explained later.
When making a decision, the programmer must specify one or more
conditions to be evaluated or tested by the program, along with statements
to be executed if the condition is determined to be True, and optionally,
other statements to be executed if the condition is False.
4
Python if Conditions
No Statement
If condition : statement
1
if condition : block of statements else: another of statements
2
if condition 1: statements elif condition 2: statements
3
elif expression 3: statements else: statements
Match .. Case
4
5
Python if Conditions
The if statement consists of the keyword if, followed by a colon (:) and a
test expression (the condition).
The body of the decision (the statement(s) that form the answer to the
condition) that follows consists either of a single statement or of multiple
statements.
If expression :
statement
6
Python if Conditions
Important Notes:
The body of the if statement must
be indented (usually by 4 spaces or a tab). This
indentation is how Python knows which statements
belong to the if block.
If the body contains only one statement, it can be
written on the same line as the if, but it is more
common and recommended to write it on a new line
with indentation.
If the body contains multiple statements, they must
all be indented at the same level.
7
Python if Conditions
8
Python if Conditions
9
Python if Conditions
10
Python if Conditions
11
Python if Conditions
12
Python if Conditions
13
Python if Conditions
Q: Write program >>>>> y = 3x + b when b = 5
14
Python if Conditions
Q: Write program >>>>> y = 5x + 2 / d when d != 0
15
Python if Conditions
16
Python if Conditions
17
Python if Conditions
18
Python if Conditions
19
Python if else Conditions
The if statement consists of the keyword if, followed by a test expression
(the condition) enclosed in parentheses, and a colon (:).
The body of the decision (the statement(s) for the first condition) that
follows consists either of a single statement or of multiple statements.
After the if block, the else keyword is written, followed by a colon (:),
and then the body of the second decision (the statement(s) for the second
condition) which also consists either of a single statement or of multiple
statements.
20
Python if else Conditions
21
Python if else Conditions
If expression :
statement(s) will execute if the expression is true
else :
statement(s) will execute if the expression is false
22
Python if else Conditions
23
Python if else Conditions
24
Python if else Conditions
25
Python if else Conditions
26
Python if else Conditions
27
Python if else Conditions
28
Python if else Conditions
29
Python if else Conditions
30
Python if else Conditions
31
Python if else Conditions
Let’s write Python code in which we will increment the salary of employee 10% if
the salary is greater than 8000. If the salary of employee is less than 8000,
will increment 15%.
32
Python if else Conditions
(5 − x )if ( 2
x 0)
y=
3
( 2 x )if ( x 0)
33
Python Nested if Statements
34
Python Nested if Statements
35
Python Nested if Statements
36
Python Nested if Statements
37
Python The elif Conditions
The if statement consists of the keyword if, followed by a test expression
(the first condition) enclosed in parentheses, followed by the body of the
decision (the statement(s) for the first condition) which consists either of a
single statement or of multiple statements.
Then comes the second if statement (using elif), followed by a test
expression (the second condition) enclosed in parentheses, followed by the
body of the decision (the statement(s) for the second condition) which
consists either of a single statement or of multiple statements.
38
Python The elif Conditions
Then comes the second if statement (using elif), followed by a test
expression (the second condition) enclosed in parentheses, followed by the
body of the decision (the statement(s) for the second condition) which
consists either of a single statement or of multiple statements.
The same applies for as many conditions as needed. After all the conditions
have been specified, the else statement follows, which contains a set of
code instructions that execute if none of the previous conditions are met.
39
Python The elif Conditions
40
Python The elif Conditions
if expression 1:
block of statements
elif expression 2:
block of statements
elif expression 3:
block of statements
elif expression 4:
block of statements
else:
block of statements
41
Python The elif Conditions
42
Python The elif Conditions
43
Python The elif Conditions
44
Python The elif Conditions
45
Python The elif Conditions
46
Python The elif Conditions
47
Python The elif Conditions
Let’s write code in Python to make a simple calculator to find
addition, subtraction, multiplication, and division of two numbers.
The math calculator menu is as follows:
Math Calculator Menu
– – – – – – – – – – – – – – – – –
+ => Addition
– => Subtraction
* => Multiplication
/ => Division
48
Python The elif Conditions
49
Python The elif Conditions
50
Python The elif Conditions
51
Python The elif Conditions
52
Python The elif Conditions
53
Python The elif Conditions
54