0% found this document useful (0 votes)
11 views

03 Control Statements

The document discusses control flow and decision making in Python using if, if-else, and if-elif-else statements. It provides the syntax for each statement and examples of how they work. The if statement executes code if a test expression is true, if-else executes the if block for true and else block for false, and if-elif-else allows checking multiple expressions and executing one of the blocks. Indentation is used to indicate the code blocks for each statement.

Uploaded by

tamanranya234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

03 Control Statements

The document discusses control flow and decision making in Python using if, if-else, and if-elif-else statements. It provides the syntax for each statement and examples of how they work. The if statement executes code if a test expression is true, if-else executes the if block for true and else block for false, and if-elif-else allows checking multiple expressions and executing one of the blocks. Indentation is used to indicate the code blocks for each statement.

Uploaded by

tamanranya234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5/13/2023

Assist. Lecturer: Hersh Mustafa Hama

[email protected]

Spring Semester 2023

Control Flow: Decision Making


 It is required when we want to execute a code only if a certain condition is satisfied.
 The if…elif…else statement is used in Python for decision making.
 Python if Statement Syntax:
If Test Expression:
statement(s)
 Here, the program evaluates the
test expression and will execute
statement(s) only if the test expression is True.
 If the test expression is False, the statement(s) is not executed.
2

1
5/13/2023

if Statement: Example
 In Python, the body of the if statement is indicated by the indentation.
 The body starts with an indentation.
 The first un-indented line marks the end.
 In the below program, var1 > 0 and var2 > 0 is the test expression.
 The body of the first if statement is executed, because the evaluate is True.

If-else Statement
 Syntax of if-else:
if Test Expression:
Body of if
else:
Body of else
 The if-else statement evaluates
Test Expression and will execute the body
of if only when the test condition is True.
 If the condition is False, the body of else is executed.
 Indentation is used to separate the blocks. 4

2
5/13/2023

If-else Statement: Example


 In the below example, the program will ask the user to enter an number.
 When the user enter a number, it convert to float type.
 If the test expression is true and the body of if is executed and the body of
else is skipped.
 Otherwise, the test expression is false and the body of else is executed and
the body of if is skipped.

If-elif-else Statement
 Syntax of if-else:
if Test Expression:
Body of if
elif Test Expression:
Body of elif
else:
Body of else
 The elif is short for else if.
 It allows to check for multiple expressions.
6

You might also like