0% found this document useful (0 votes)
5 views5 pages

Chapter 9 - Flow of Control

The document discusses the flow of control in programming, detailing sequential, conditional, and iterative flows. It explains various conditional statements such as if, if-else, and nested if, along with the use of the range() function and iterative statements like for and while loops. Additionally, it covers break and continue statements, as well as nested loops with examples for clarity.

Uploaded by

yukesh1739
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)
5 views5 pages

Chapter 9 - Flow of Control

The document discusses the flow of control in programming, detailing sequential, conditional, and iterative flows. It explains various conditional statements such as if, if-else, and nested if, along with the use of the range() function and iterative statements like for and while loops. Additionally, it covers break and continue statements, as well as nested loops with examples for clarity.

Uploaded by

yukesh1739
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/ 5

VELAMMAL VIDHYASHRAM - MARAIMALAI NAGAR

Chapter 9 - FLOW OF CONTROL

Flow of Control

Flow of Control refers to the order in which statements are executed in a program.

Sequential Flow
The default control flow in a program is sequential flow, in which statements are executed
line-by-line one after the other in a sequence in which they are written.

Conditional Flow
Conditional flow refers to execution of certain statements only if a specific condition is met.
This is accomplished by the use of conditional statements such as if, if-else, and if-elif-else.

Iterative Flow(Looping):
Iteration means 'repetition'.
Iterative flow repeats statements in a block of code. Repetition or looping can be performed
a fixed number of times or until a certain condition is met. This is accomplished through the
use of iterative statements: for and while.

Conditional Statements

if statement:
The if statement is used to execute a block of code only if a certain condition is true.

Syntax:
if <condition>:
Set of Statements

Example:

age = int(input("Enter your age : "))


if age>=18:
print('You are allowed to vote')

if-else statement:
The if-else statement is used to execute one block of code if a certain condition is true, and
another block of code if the condition is false.

Syntax of if-else
if <condition>:
set of statements
else:
set of statements
Example:

age = int(input("Enter your age : "))


if age>=18:
print('You are allowed to vote')
else:
print('You are not allowed to vote')

if-elif-else statement:

if-elif-else statement is used to check multiple conditions.

Syntax of if-elif else

if <condition>:
set of statements
elif <condition>:
set of statements
elif <condition>:
set of statements
elif <condition>:
set of statements
.
.
.
else:
set of statements

Example:
per = int(input("Enter Percentage : "))
if per >= 75:
print("Distinction")
elif per >= 60:
print("Grade-A")
elif per >= 50:
print("Grade-B")
elif per >= 40:
print("Grade-C")
else:
print("Grade-D")

Nested if statement:
An if statements inside if statements, this is called nested if statements.
Example:
x=9
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
else:
print("Below 10")

range( ) Function
range( ) is a built-in function that returns a sequence of numbers.
Syntax
range(start, stop, step)
start: The starting value of the sequence (inclusive). If not specified, it defaults to 0.
stop: The ending value of the sequence (exclusive).
step: The difference between two consecutive elements. Its default value is 1.
The range( ) function can be used in for loops to iterate over a sequence of numbers.

Case-1
x = list(range(5))
print(x)
Output
[0, 1, 2, 3, 4]

Case-2
x = list(range(3,6))
print(x)
Output
[3, 4, 5]

Case-3
x = list(range(3,20,2))
print(x)
Output
[3, 5, 7, 9, 11, 13, 15, 17,
19]

Case-4
x = list(range(0, -9, -1))
print(x)
Output
[0, -1, -2, -3, -4, -5, -6, -7, -
8]

Iterative Statement

Iterative statements execute a set of instructions multiple times. 'for' and 'while' loops are
the iterative statements in Python.
while Loop
The while loop repeatedly executes a block of code as long as the specified condition is
true.
The while loop is an exit controlled loop, i.e. the user is responsible for exiting the loop by
changing the value of the condition to False.
Syntax:
while (condition):
block of statements

Example:
Write a Python Program (using a while loop) to Find the Sum of First 10 Natural Numbers.
num=1
sum=0
while (num <= 10):
sum = sum + num
num = num+1
print ("Sum of Natural Numbers : ", sum)

for Loop
The for loop is used to iterate over a sequence (such as a list, tuple, string, etc.) and
execute a block of code for each item in the sequence.
Syntax
for <control_variable> in <sequence>:
Block of code

Example:
Write a Python Program (using for loop) to Find the Sum of First 10 Natural Numbers.
sum=0
for num in range(1,11):
sum = sum + num
print ("Sum of Natural Numbers : ", sum)
Output:
Sum of Natural Numbers : 55

Break and Continue Statement


Break Statement: The break statement is used to terminate a loop immediately. It is
typically used with conditional statements.
Example:
fruits = ['apple', 'banana', 'cherry']
for x in fruits:
if x == 'banana':
break
print(x)

Continue Statement: The continue statement skips all the remaining statements in the
current iteration of the loop and moves the control to the beginning of the next iteration.
Example:
Write a program in Python to print all natural numbers from 1 to 10 except 7.
for i in range(1,11):
if i==7:
continue
print(i)

Nested loops
Nested loops refers to a loop within a loop.

Example:
n = int(input("Enter the number of rows: "))
for i in range(n):
for j in range(i+1):
print("*", end="")
print()

Output
Enter the number of rows: 5
*
**
***
****
*****

You might also like