Module5 - Control Structure
Module5 - Control Structure
COURSE CODE:
(COS201)
COURSE LECTURERS
First Semester
2024/2025 Session
OBJECTIVES
The expected outcomes from the students at the end
of this chapter are:
i. discuss this concept of "Program control Structure"
ii. define conditional statement
iii. list and explain the forms of Python conditional
statement
iv. apply the conditional statement in Python
Programming
v. define Loop control statement in Python
vi. list and explain the categories of Loop control statement
vii. apply Loop control statement in Python programming 2
INTRODUCTION
Sequential execution is not usually the case in
programming. A program may sometimes decide to
branch to another section or repeat the current
section of code, depending on the decision taking from
the code control statement. Python, like other
languages used in programming, has such control
statements that can be used to direct the execution of
program statements. This control statement makes
the program execution disobey the law of sequential
execution of program codes. The control statement
decides:
If a code will run or not run
If the line of code(s) will be repeated or not repeated
If the control will jump to another line of code. 3
BRANCHING MECHANISMS
Branching is the process of using a control statement to make
a decision on which section of a code will be executed. The
programme execution takes place only after satisfying the
stated condition. This gives the programme the power to
make decisions and adapt to different scenarios. Control
statement has other names such as conditional, selective,
decision statements and others. In Python, the commonly
used control statements are:
if _then statement
if_then_else statement
if_eLif_else
4
THE IF_THEN STATEMENT
The if_then statement is usually used to control programme
execution;
It is a statement used to test whether a transfer of control or
execution will take place or not.
It is a powerful statement that gives the computer the ability to
make decisions and take alternative action based on the outcome.
It is always used in conjunction with relational operators to set the
condition that will be tested.
The if_then control statement in Python is the same thing that is
obtained in other languages used in programming.
It is only the syntax that differs. This statement is used to make
decision based on the given condition. 5
The if_then control statement executes only if the condition is true.
IF_THEN STATEMENT SYNTAX
if_then Statement Syntax:
if Condition
statements_sequence
6
IF_THEN STATEMENT FLOWCHART
7
IF-THEN STATEMENT EXAMPLE
# Define a variable
temperature = 30
9
IF/ELSE STATEMENT SYNTAX
if Conditional statement:
statements_sequence 1
else:
Statements_sequence 2
12
IF/ELIF STATEMENT SYNTAX
if conditional Statement1:
Statement1
elif conditional Statement2:
Statement 2
else:
Statement 3
In the above syntax, when the conditional statement1 is true, then statement1 will
be executed. When the conditional statement1 is false, then the conditional
statement2 will be checked. When the conditional statement2 is true, then 13
tatement2 will be executed; otherwise, statement3 will be executed.
IF-ELIF-ELSE STATEMENT EXAMPLE
# Define a variable
temperature = 20
# Outer if condition
if temperature > 25:
print("It's warm outside.")
# Nested if condition
if humidity > 70:
print("It's also humid.")
else:
print("The air is dry.")
else:
print("It's cool outside.")
16
THANK YOU
17