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

Flow-Of-Control

Python

Uploaded by

Afreed Shahid
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)
11 views6 pages

Flow-Of-Control

Python

Uploaded by

Afreed Shahid
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

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

The syntax is if condition:


statement(s)

For example, if age >= 18:


print(“Eligible to vote”)
Enter the number: 10
#Program to find the greatest of two numbers
Enter the another number: 20
a = int(input("Enter the number: "))
b = int(input("Enter the another number: ")) Greates is 20
big = a
if b > big:
big = b
print("Greates is ", big)

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)

For example, a = int(input("Enter the number: "))


Enter the number: 10
b = int(input("Enter another number: "))
Enter another number: 20
if a > b:
print("Greates is ", a) Greates is 20
else:
print("Greatest is ", b)

#Program to determine whether a voter is eligible to vote


Enter the age: 17
age = int(input("Enter the age: "))
Ineligible to vote

if age >= 18:


print("Eligible to vote ") Enter the age: 20
else: Eligible to vote
print("Ineligible to vote ")
#Program to find the positive difference between two numbers
a = int(input("Enter the number: "))
b= int(input("Enter another number: "))
Enter the number: 10
if a > b:
Enter another number: 20
d=a-b
Difference is 10
else:
d=b-a
print("Difference is ", d)

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.

#Program to check whether the number is positive, negative or 0


n = int(input("Enter the number: "))
Enter the number: -5
if n > 0: -5 is negative
print(n, " is positive")
elif n < 0: Enter the number: 5
print(n, " is negative") 5 is positive
else:
print("It is zero") Enter the number: 0
It is zero
#Display the appropriate message as per the colour
#of signal at the road crossing.
signal = input("Enter the signal: ") Enter the signal: RED
if(signal == "red" or signal == "RED"): STOP
print("STOP")
elif (signal == "orange" or signal == "ORANGE"): Enter the signal: orange
print("Be Slow") Be Slow
elif (signal == "green" or signal == "GREEN"):
print("go")
 We can have an if..else condition within elif block. It is called as nested if. We can write many levels
of nesting inside if..else statemets.

#Program to create four function calculator


n1 = float(input("Enter the number: "))
n2 = float(input("Enter anoter number: "))

opr = input("Enter the operator(+, -, *, /): ")

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.

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


b = int(input("Enter another number: "))
if a > b:
print("Greates is ", a)
print("Smallest is ", b)
else:
print("Greatest is ", b)
print("Smallest is ", a)

Repetition

 The process of repeated execution a set of statements is called as loop or repetition.


 Looping constructs (statements) repeat the execution of a set of statements as long as a particular
condition remains true.
 There are two looping constructs: for and while

for statement

 This statement iterates over a range of values


or a sequence.
 for loop executes for each of the items in the
range and these values can be either numeric
or characters or elements of data type like
strings or tuple.
 The control variable checks whether each
value in the range is traversed or not.
 When all the values in the range is exhausted,
the control is then transferred statement
following the for loop.
 In this loop, we know in advance the number
of times the loop is repeated.
 Syntax is
for <control _variable> in <sequence/range>:

<Statements inside the body> Enter the string: King


#Print the characters of the string K
st = input("Enter the string: ") i
for ch in st: n
print(ch) g
#Print the numbers in sequence 10
count = [10, 20, 30, 40, 50] 20
for n in count: 30
print(n) 40
50

#Print even numbers in sequence


2
count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 4
for n in count: 6
if n % 2 == 0: 8
print(n) 10

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

 This statement executes a block of code repeatedly as long as


the control condition is true.]
 For every iteration, the control condition is tested before any
statement inside the loop is executed.
 When the control condition becomes false, the control is
transferred to statement following body of the loop.
 If the control condition is initially false, the body is not
executed even once.
 The syntax is while test_condition:
body_of_the_loop
1
#Print first 5 natural numbers 2
count = 1 3
while count <= 5: 4
print(count) 5
count = count + 1

#Print divisors or factors of a number


#Print divisors or factors of a number Enter the number: 100
n = int(input("Enter the number: ")) 2 4 5 10 20 25 50
count = 2
while count <= int(n/2):
if n % count == 0:
print(count, end =' ')
count = count + 1

Break and Continue statements

 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=' ')

#Sum of positive numbers and a negative number stop the loop.


sum = 0
print("Enter the numbers to find sum and negative number ends the loop")
while True:
num = int(input()) Enter the numbers to find sum and negative number ends
if num < 0: the loop
break; 10
else: 20
sum += num 30
print("Sum is ", sum) 40
50
-1
Sum is 150

#To determine whether the given number is prime


n = int(input("Enter the number: "))
flag = True Enter the number: 29
for count in range(2, int(n/2)):
29 is a prime
if n % count == 0:
flag = False
break Enter the number: 105
if flag: 105 is not a prime
print(n, " is a prime")
else:
print(n, " is not a prime")
Continue statement

 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.

#Print the numbers except 3


for count in range(6):
01245
if(count == 3):
continue
print(count, end=' ')

Nested loops

A loop inside another loop is called as nested loop.


1
for outer in range(1,4): 12
for inner in range(1, outer+1): 123
print(inner, end = ' ')
print("\n")

Any type of loop can be nested.

#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)

You might also like