Flow-Of-Control
Flow-Of-Control
If the statements of a program are executed in an order in which they are written, it is called as
sequence.
The order of execution of the statements in program is known as flow of control.
Control structures can be used to implement flow of control.
There are two types of control structures: selection and repetition
Selection
Selection is the concept of deciding whether a statement or group of statements should be executed
or not.
The selection is based on a condition.
Selection is implemented using if statement and if-else statement.
if statement
if – else statement
It has two alternative paths and condition determines which path gets executed.
The syntax is if condition:
statement(s)
else:
statement(s)
if – elif statement
There are multiple alternatives that require multiple conditions.
elif means else .. if
Syntax is if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
If the first condition is false, then the second condition is checked. If the second condition is false,
then the third condition is checked and so on. If one of the conditions is true, then the
corresponding block is executed and the if statement terminates.
result = 0
if opr == '+':
result = n1 + n2
elif opr == '-':
if n1 > n2:
result = n1 - n2
Enter the number: 10
else:
Enter anoter number: 20
result = n2 - n1
elif opr == '*': Enter the operator(+, -, *, /): /
result = n1 * n2 0.5
elif opr == '/':
if n2 == 0:
print("Division by zero not allowed")
else:
result = n1 / n2
else:
print("Wrong input")
print(result)
Indentation
Leading space (spaces or tabs) at the beginning of the statement is called as indentation.
We use indentation for blocks and nested blocks.
The same level of indentation associates statements into a single block of code.
The interpreter strictly checks the indentation levels and give errors if indentation is not correct.
Use single tab for each level of indentation.
Repetition
for statement
Range() function
It is built-in function.
Syntax 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 step value.
start, stop and step are the parameters to the range() function.
start and step parameters are optional.
The default value of start is 0 and step is 1.
All the parameters of range() function are integers and step parameter can be positive or negative,
excluding 0.
0
#start and step are not specified 1
for n in range(5): 2
print(n) 3
4
5
#default step value 1 is 6
for n in range(5, 10): 7
print(n) 8
9
5
10
#step value is 5
15
for n in range(5, 20, 5):
print(n)
-1
#step value is -1, hence decreasing sequence -2
for n in range(-1, -5, -1): -3
0
print(n) -4
10
#Print the multiples of 10
20
for n in range(5): 30
print(10 * n) 40
while statement
Sometimes we may want to exit from the loop when a condition is occurs (exit statement) or
skip some statements continue the execution of the loop (continue statement).
These requirements can be achieved by using break and continue statements.
break statement
This statement terminates the current loop resumes the execution of statements following that
loop.
#Break statement
for count in range(10):
0 1 2 3 4
if count == 5:
break;
else:
print(count, end=' ')
The continue statement skips the execution of remaining statements inside the body of the
loop for the current iteration and jumps to the biginning of the loop for next iteration.
Nested loops
#Factorial of a number
n = int(input("Enter the number: "))
if n < 0:
print("Wrong input")
elif n == 0:
print("Factorial of 0 is 1") Enter the number: 5
else: Factorial is 120
fact =1
for count in range(1, n+1):
fact *= count
print("Factorial is ", fact)