0% found this document useful (0 votes)
822 views8 pages

Control Flow Statements and Loops

The document discusses different types of control flow statements in Python including conditional statements like if, elif, else and iterative statements like for and while loops. It provides examples and explanations of how if, if-else and if-elif-else statements work in Python by evaluating different conditions and executing the appropriate code blocks.

Uploaded by

Noble Prince
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)
822 views8 pages

Control Flow Statements and Loops

The document discusses different types of control flow statements in Python including conditional statements like if, elif, else and iterative statements like for and while loops. It provides examples and explanations of how if, if-else and if-elif-else statements work in Python by evaluating different conditions and executing the appropriate code blocks.

Uploaded by

Noble Prince
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/ 8

Control Flow Statements and Loops

In Python programming, flow control is the order in which statements or blocks of code are executed at runtime based on a
condition.

The flow control statements are divided into three categories

1. Conditional statements
2. Iterative statements.
3. Transfer statements

Python control flow statements

Conditional statements
In Python, condition statements act depending on whether a given condition is true or false. You can execute different blocks of
codes depending on the outcome of a condition. Condition statements always evaluate to either True or False.
For example, assigning grades (A, B, C) based on marks obtained by a student.
1. if the percentage is above 90, assign grade A
2. if the percentage is above 75, assign grade B
3. if the percentage is above 65, assign grade C
In Python, there are three forms of the if...else statement.
1. if statement
2. if...else statement
3. if...elif...else statement
Python if statement
The syntax of if statement in Python is:

if condition:
# body of if statement

The if statement evaluates condition.


1. If condition is evaluated to True, the code inside the body of if is executed.
2. If condition is evaluated to False, the code inside the body of if is skipped.

Working of if Statement

Example 1: Python if Statement


number = 10

# check if number is greater than 0


if number > 0:
print('Number is positive.')

print('The if statement is easy')


Output
Number is positive.
The if statement is easy

In the above example, we have created a variable named number. Notice the test condition,

number > 0

Here, since number is greater than 0, the condition evaluates True.


If we change the value of variable to a negative integer. Let's say -5.

number = -5

Now, when we run the program, the output will be:


The if statement is easy

This is because the value of number is less than 0. Hence, the condition evaluates to False. And, the body of if block is skipped.
Python if...else Statement
An if statement can have an optional else clause.
The syntax of if...else statement is:

if condition:
# block of code if condition is True

else:
# block of code if condition is False

The if...else statement evaluates the given condition:


If the condition evaluates to True,
• the code inside if is executed
• the code inside else is skipped
If the condition evaluates to False,
• the code inside else is executed
• the code inside if is skipped

Working of if...else Statement

Example 2. Python if...else Statement


number = 10

if number > 0:
print('Positive number')
else:
print('Not a Positive number')

print('This statement is always executed')


Output
Positive number
This statement is always executed
In the above example, we have created a variable named number. Notice the test condition,

number > 0

Since the value of number is 10, the test condition evaluates to True. Hence code inside the body of if is executed.
If we change the value of variable to a negative integer. Let's say -5.

number = -5

Now if we run the program, the output will be:

Not a Positive Number.


This statement is always executed.

Here, the test condition evaluates to False. Hence code inside the body of else is executed.
Python if...elif...else Statement
The if...else statement is used to execute a block of code among two alternatives.
However, if we need to make a choice between more than two alternatives, then we use the if...elif...else statement.
The syntax of the if...elif...else statement is:

if condition1:
# code block 1

elif condition2:
# code block 2

else:
# code block 3

Here,
1. If condition1 evaluates to true, code block 1 is executed.
2. If condition1 evaluates to false, then condition2 is evaluated.
a. If condition2 is true, code block 2 is executed.
b. If condition2 is false, code block 3 is executed.

Working of if...elif Statement

Example 3: Python if...elif...else Statement


number = 0

if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')

print('This statement is always executed')


Output

Zero
This statement is always executed

In the above example, we have created a variable named number with the value 0. Here, we have two condition expressions:
Here, both the conditions evaluate to False. Hence the statement inside the body of else is executed.
Python Nested if statements
We can also use an if statement inside of an if statement. This is known as a nested if statement.
The syntax of nested if statement is:

# outer if statement
if condition1:
# statement(s)

# inner if statement
if condition2:
# statement(s)

Notes:
• We can add else and elif statements to the inner if statement as required.
• We can also insert inner if statement inside the outer else or elif statements(if they exist)
• We can nest multiple layers of if statements.

Example 4: Python Nested if Statement


number = 5

# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')

# outer else statement


else:
print('Number is not positive')

# Output: Number is positive


In the above example, we have used a nested if statement to check whether the given number is positive, negative, or 0.

You might also like