Flow of Control
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)
if condition:
statement(s)
else:
statement(s)
1
C2TI Flow of Control
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
elif op == "*":
2
C2TI Flow of Control
elif op == "/":
if val2 == 0:
else:
• Output:
Enter value 1: 84
Enter value 2: 4
➢ 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.
4
C2TI Flow of Control
while test_condition:
body of while
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).