Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Module5 - Control Structure

Uploaded by

Blossomlove12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Module5 - Control Structure

Uploaded by

Blossomlove12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

COMPUTER PROGRAMMING I

PROGRAM CONTROL STRUCTURE

COURSE CODE:
(COS201)

COURSE LECTURERS

1 Dr. Olaniyan Deborah


2 Dr. Julius Olaniyan

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

If the conditional statement is true, the statement


will be executed. It will not be executed. The conditional
statements must always evaluate to true or false.

6
IF_THEN STATEMENT FLOWCHART

7
IF-THEN STATEMENT EXAMPLE

# Define a variable
temperature = 30

# if-then statement to check if the temperature is


above 25
if temperature > 25:
print("It's a hot day!") # This will print if
temperature is above 25
8
THE IF AND ELSE STATEMENT
The if and else Statement
This is used to choose between two choices. The first statement is
executed if the condition is true; otherwise, the second statement
will be executed

9
IF/ELSE STATEMENT SYNTAX
if Conditional statement:
statements_sequence 1
else:
Statements_sequence 2

 Statements_sequence 1 will be executed if the


conditional statement evaluates to true otherwise
statements_sequence 2 will be executed.
10
IF-ELSE STATEMENT EXAMPLE
# Define a variable
temperature = 15

# if-else statement to check temperature


if temperature > 25:
print("It's a hot day!")
else:
print("It's a cool day.")
11
IF AND ELIF STATEMENT
 When there are more than two options to choose from
in a programme statement, the if/elif is used to achieve
that purpose.
 Unlike the if/else that can be applied to choose
between two options, if/elif is used when there are
more than two options.

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

if temperature > 30:


print("It's very hot!")
elif temperature > 20:
print("It's a warm day.")
else:
print("It's a bit chilly.")
 If temperature > 30 is True, the first block will run.
 If temperature > 30 is False but temperature > 20 is True, the elif block will run.
 If both conditions are False, the else block will execute.
14
NESTED-IF COMMAND
 This is a situation where if command is hosting
another if command.
 A nested if statement in Python is an if statement
inside another if statement. This allows for more
complex decision-making, where you check additional
conditions only if the first condition is met.
 Other types of if commands can also be embedded
inside the main if command depending on what
the programmer wants to achieve. Note that the
indentation matters a lot in separating the
15
hierarchy.
NESTED IF COMMAND EXAMPLE
 # Define a variable for temperature
 temperature = 30
 humidity = 80

 # 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

You might also like