0% found this document useful (0 votes)
12 views6 pages

Flow of Control

The document explains the flow of control in programming, focusing on control structures in Python, which include selection (if, if..else, and elif statements) and repetition (for and while loops). It details the syntax and usage of these structures, including indentation rules and the use of break and continue statements for loop control. Additionally, it covers nested loops and the range() function for generating sequences in for loops.

Uploaded by

Neeta
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)
12 views6 pages

Flow of Control

The document explains the flow of control in programming, focusing on control structures in Python, which include selection (if, if..else, and elif statements) and repetition (for and while loops). It details the syntax and usage of these structures, including indentation rules and the use of break and continue statements for loop control. Additionally, it covers nested loops and the range() function for generating sequences in for loops.

Uploaded by

Neeta
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/ 6

C2TI Flow of Control

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 and repetition.
➢ Selection:
• A decision involves selecting from one of the two or more possible
options.
• In programming, this concept of decision making or selection is
implemented with the help of if..else statement.
• Now, suppose we want to display the positive difference of the two
numbers num1 and num2 (given).
• For that, we need to modify our approach.
• The syntax of if statement is:

if condition:

statement(s)

• The indentation implies that its execution is dependent on the condition.


• There is no limit on the number of statements that can appear as a block
under the if statement.
• A variant of if statement called if..else statement allows us to write two
alternative paths and the control condition determines which path gets
executed.
• The syntax for if..else statement is as follows:

if condition:

statement(s)

else:

statement(s)

• Many a times there are situations that require multiple conditions to


be checked and it may lead to many alternatives.
• In such cases we can chain the conditions using if..elif (elif means
else..if).

1
C2TI Flow of Control

• The syntax for a selection structure using elif is as shown below.

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.
• In the program, for the operators "-" and "/", there exists an if..else
condition within the elif block.
• This is called nested if.
• We can have many levels of nesting inside if..else statements.
• Example:
#Program to create a four function calculator
result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1

elif op == "*":

2
C2TI Flow of Control

result = val1 * val2

elif op == "/":

if val2 == 0:

print("Error! Division by zero is not allowed.


Program terminated")

else: result = val1/val2

else:

print("Wrong input, program terminated")

print("The result is ",result)

• Output:

Enter value 1: 84

Enter value 2: 4

Enter any one of the operator (+,-,*,/): /

The result is 21.0

➢ Indentation:
• In most programming languages, the statements within a block are put inside
curly brackets.
• However, Python uses indentation for block as well as for nested block
structures.
• 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.
➢ Repetition:
• This kind of repetition is also called iteration.
• 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 particular
logical condition remains true.

3
C2TI Flow of Control

•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.
• It is the responsibility of the programmer to ensure that this condition
eventually does become false so that there is an exit condition and it does
not become an infinite loop.
➢ The ‘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, as we shall see in later chapters, 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 for loop, it is known in advance the number of times the loop will
execute.

(A) Syntax of the For Loop:

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

<statements inside body of the loop>

(B) The Range() Function:

• The range() is a built-in function in Python. Syntax of range() function is:

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

• It is used to create a list containing a sequence of integers from the given


start value upto stop value (excluding stop value), with a difference of the
given step value.
• To begin with, simply remember that function takes parameters to work on.
• In function range(), start, stop and step are parameters.
• The start and step parameters are optional.
• If start value is not specified, by default the list starts from 0.
• If step is also not specified, by default the value increases by 1 in each
iteration.
• All parameters of range() function must be integers.

4
C2TI Flow of Control

• 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.
➢ The ‘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 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:

while test_condition:

body of while

• Body of the loop is indented with respect to the while statement.


• Similarly, the statements within if are indented with respect to positioning of
if statement.
➢ Break and Continue Statement:
• Looping constructs allow programmers to repeat tasks efficiently.
• 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.
• Python provides these statements as a tool to give more flexibility to the
programmer to control the flow of execution of a program.
➢ 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.
• When the break statement is executed and the for loop terminates.

5
C2TI Flow of Control

➢ 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.
• The loop continues after the continue statement to print other values till the
for loop terminates.
➢ Nested Loops:
• A loop may contain another loop inside it. A loop inside another 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).

ALL THE BEST

You might also like