# Flow of Control in Python Class 9 Notes

Teachers and Examiners (CBSESkillEduction) collaborated to create the Flow of Control in Python Class 9 Notes. All the important Information are taken from the NCERT Textbook Artificial Intelligence (417).

Contents

## Flow of Control in Python Class 9 Notes

There are three control flow statements in Python – if, for and while.

### Decision Making Statement

In programming languages, decision-making statements determine the program’s execution flow. Python has the following decision-making statements:

1. if statement
2. if..else statements

#### If Statement

The if statement is used to test a condition: if the condition is true, a set of statements is executed (called the if-block).

Flow of Control in Python Class 9 Notes

``````Syntax -
test expression:
statement(s)``````

Flow of Control in Python Class 9 Notes

``````# Check if the number is positive, we print an appropriate message

num = 3
if num > 0:
print(num, “is a positive number.”)
print(“this is always printed”)
num = -1
if num > 0:
print(num, “is a positive number.”)
print(“this is always printed”)``````

#### If…else statement

The if/else statement is a control flow statement that allows you to run a block of code only if a set of conditions are satisfied.

Flow of Control in Python Class 9 Notes

``````Syntax -

if test expression:
Body of if
else:
Body of else``````

Flow of Control in Python Class 9 Notes

``````# A program to check if a person can vote

if age >= 18:
print(“You are eligible to vote”)
else:
print(“You are not eligible to vote”)``````

Flow of Control in Python Class 9 Notes

``````# Write Python program to find the greatest number among two numbers

num1 = int(input(“Enter Number”));
num2 = int(input(“Enter Number”));
if num1 >= num2:
if num1 == num2:
print("Both numbers are equal.")
else:
print("Fisrt number is greater than the second number.")
else:
print("Second number is greater than the First number.")``````

Flow of Control in Python Class 9 Notes

``````# Write python program to check the number is even or odd

num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))``````

Elif stands for “else if.” It enables us to check for several expressions at the same time. If the if condition is False, the next elif block’s condition is checked, and so on. The body of else is executed if all of the conditions are False.

Flow of Control in Python Class 9 Notes

``````Syntax -

if test expression:
Body of if
elif test expression:
Body of elif
else: Body of else``````

Flow of Control in Python Class 9 Notes

``````# To check the grade of a student

Marks = 60
if marks > 75:
elif marks > 60:
else:

#### Nested if statements

An if…elif…else sentence can be nestled inside another if…elif…else statement. In computer programming, this is referred to as nesting.

Flow of Control in Python Class 9 Notes

``````# Write a program to check weather number is zero, positive or negative

num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")``````

#### For Loop

The for statement allows you to specify how many times a statement or compound statement should be repeated. A for statement’s body is executed one or more times until an optional condition is met.

``````Syntax -

for val in sequence:
Body of for``````
``````# Program to find the sum of all numbers stored in a list

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)``````

#### While Statement

The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what is called a looping statement. A while statement can have an optional else clause.

``````Syntax -

while test_expression:
Body of while``````

Flow of Control in Python Class 9 Notes

``````# Program to add natural

n = int(input("Enter n: "))
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1
print("The sum is", sum)``````