0% found this document useful (0 votes)
50 views7 pages

6 Introduction To Flow of Control

The document provides an introduction to flow of control in Python, detailing control structures such as selection and repetition. It explains the importance of indentation, various types of flow including sequential, conditional, and iterative, along with control statements like if, if-else, and loops (for and while). Additionally, it covers break and continue statements, nested loops, and includes practical examples for sorting numbers, generating patterns, calculating series sums, and finding factorials.

Uploaded by

Kiran Raval
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)
50 views7 pages

6 Introduction To Flow of Control

The document provides an introduction to flow of control in Python, detailing control structures such as selection and repetition. It explains the importance of indentation, various types of flow including sequential, conditional, and iterative, along with control statements like if, if-else, and loops (for and while). Additionally, it covers break and continue statements, nested loops, and includes practical examples for sorting numbers, generating patterns, calculating series sums, and finding factorials.

Uploaded by

Kiran Raval
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/ 7

Introduction to flow of control

The order of execution of the statements in a program is known as flow of control.


The flow of control can be implemented using control structures. Python supports
two types of control structures:

 Selection
 Repetition

Indentation
Python uses indentation to indicate a block of code.

Leading whitespace (spaces and tabs) at the beginning of a statement is called


indentation.

 In Python, the same level of indentation associates statements into a single


block of code.
 The interpreter checks indentation levels very strictly and throws up syntax
errors if indentation is not correct.
 It is a common practice to use a single tab for each level of indentation.
 Python uses indentation for block as well as for nested block structures.

Sequential flow
In sequential flow of control, statements are executed one after the other in order.

Conditional flow
When the flow of control of a program is changed based on some condition using
control statements, it is termed conditional flow of control.

Iterative flow
In iterative flow of control, a statement or block is executed until the program reaches
a certain state, or operations have been applied to every element of a collection.

A decision involves selecting from one of the two or more possible options.
The if statement is used for selection or decision making.
Conditional Statements
1. If statement
Syntax:
if condition:
statement(s)

If the condition is true, then the indented statement(s) are executed. The
indentation implies that its execution is dependent on the condition.

2. If....else statement
Syntax:

if condition:
statement{s)
else:
statement(s)

If the condition is true, then the first indented statements) are executed. If the
condition is false, then the second indented statements) are executed.

3. If...elif...else
Syntax:

if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)

Number of elif is dependent on the number of conditions to be checked. If the first


condition is false, then the next condition is checked, and so on. If one of the
conditions is true, then the corresponding indented block executes, and the if
statement terminates.

Repetition
Iteration / repetition refers to the execution of the same code multiple times in
succession.
Repetition of a set of statements in a program is made possible using looping
constructs.
 Looping constructs provide the facility to execute a set of statements in a
program repetitively, based on a condition.
 The statements in a loop are executed again and again as long as the
particular logical condition remains true.
 This condition is checked based on the value of a variable called the loop's
control variable.
 When the condition becomes false, the loop terminates.

Iterative Statements
1. For loop
The for statement is used to iterate over a range of values or a sequence. The for
loop is executed for each of the items in the range. These values can be either
numeric, or they can be elements of a data type like a string, list, or tuple.

 With every iteration of the loop, the control variable checks whether each of
the values in the range have been traversed or not.
 When all the items in the range are exhausted, the statements within loop are
not executed; the control is then transferred to the statement immediately
following the for loop.
 While using a for loop, it is known in advance the number of times the loop will
execute.
Syntax of for loop in python:

for < control-variable> in ‹sequence / items in range>:

< statements inside body of the loop>

2. Range() function
Range() is a built-in function in Python. It is used to create a list containing a
sequence of integers from the given start value up to stop value (excluding stop
value), with a difference of the given step value. In function rangel), start, stop and
step are parameters.

 The start and step parameters are optional.


 If the start value is not specified, by default the list starts from O.
 If step is also not specified, by default the value increases by 1 in each
iteration.
 All parameters of range) function must be integers.
 The step parameter can be a positive or a negative integer excluding zero.
The function range() is often used in for loops for generating a sequence of numbers.
Syntax of range function python:

range([start], stop[, step])

3. While loop
The while statement executes a block of code repeatedly as long as the control
condition of the loop is true.

 The control condition of the while loop is executed before any statement
inside the loop is executed.
 After each iteration, the control condition is tested again and the loop
continues as long as the condition remains true.
 When this condition becomes false, the statements in the body of loop are not
executed and the control is transferred to the statement immediately following
the body of the while loop.
 If the condition of the while loop is initially false, the body is not executed even
once.
 The statements within the body of the while loop must ensure that the
condition eventually becomes false; otherwise the loop will become an infinite
loop, leading to a logical error in the program.

Syntax of while loop in python:

while test_condition:
body of while

4. Break and continue statement


In certain situations, when some particular condition occurs, we may want to exit
from a loop (come out of the loop forever) or skip some statements of the loop before
continuing further in the loop. These requirements can be achieved by using break
and continue statements, respectively.

Break Statement
The break statement alters the normal flow of execution as it terminates the current
loop and resumes execution of the statement following that loop.

Continue Statement
When a continue statement is encountered, the control skips the execution of
remaining statements inside the body of the loop for the current iteration and jumps
to the beginning of the loop for the next iteration. If the loop's condition is still true,
the loop is entered again, else the control is transferred to the statement immediately
following the loop.

5. Nested loops
A loop inside a loop is called a nested loop. Python does not impose any restriction
on how many loops can be nested inside a loop or on the levels of nesting. Any type
of loop (for/while) may be nested within another loop (for/while).

Important programs (Write these in practical record)

1. Sorting Three Numbers in Descending Order Using If-


Else

# Program to sort 3 numbers in descending order

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


if b >= c:
print(a, b, c)
else:
print(a, c, b)
elif b >= a and b >= c:
if a >= c:
print(b, a, c)
else:
print(b, c, a)
else:
if a >= b:
print(c, a, b)
else:
print(c, b, a)

Output -
Enter first number: 10
Enter second number: 30
Enter third number: 20
30 20 10
2. Generating the Given Pattern Based on User Input

# Program to generate the pattern 1, 12, 123, 1234, ...

n = int(input("Enter the number of rows: "))

for i in range(1, n + 1):


for j in range(1, i + 1):
print(j, end="")
print()

Output -
Enter the number of rows: 5
1
12
123
1234
12345

3. Sum of Series (1*1) + (2*2) + (3*3) + ... + (n*n)

# Program to find sum of the series (1*1) + (2*2) + (3*3) +


... + (n*n)

n = int(input("Enter a number: "))


sum_series = sum(i * i for i in range(1, n + 1))

print("Sum of the series:", sum_series)


Output -
Enter a number: 5
Sum of the series: 55

4. Factorial of a Given Number

# Program to find factorial of a given number

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

num = int(input("Enter a number: "))


print(f"Factorial of {num} is {factorial(num)}")

Output -
Enter a number: 5
Factorial of 5 is 120

You might also like