COMPUTER SCIENCE
FLOW CONTROL
Control Structures
• In sequential structure instructions of the program are executed one after the other
from the beginning till the end of the program
• The order of execution of the statements in a program is known as flow of control
• The flow of control can be implemented using the control structures
• Types of control structures,
➢ Selection
➢ Repetition
Selection structure/Conditional/Decision making
It is a programming construct, which allows to choose a set of instructions for execution
depending upon the condition.
Selection structure can be implemented using
• if statement
• if else statement
• if ..elif statement
• Nested if statement
if statement
It is a programming construct, which allows to decide whether a set of statement is to be
executed or not.
Only If the condition is true ,the indented statement(s) are executed.
Syn:
if condition:
statement(s)
Eg:
age=int(input(“enter your age=”))
if age>=18:
print(“eligible for vote”)
if else statement
It is a variant of if statement, which allows to decide whether one set of statement is to be
executed or the another.
If the condition is true ,the indented statement(s)1 are executed.
If the condition is false, the indented statement(s)2 are executed.
Syn:
if condition:
statement(s)1
else:
statement(s)2
1
COMPUTER SCIENCE
Eg:
age=int(input(“enter your age=”))
if age>=18:
print(“eligible for vote”)
else:
print(“Not eligible for vote”)
if elif statement (if..else..if)
It is a variant of if statement, which allows multiple conditions, to decide execution of a
particular set of instructions.
Number of elif is dependent on 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 condition is true, then the corresponding indented block executes, and terminates
the if statement.
If all the conditions are false, else indented statement(s) will be executed.
Syn:
if condition1:
statement(s)1
elif condition2:
statement(s)2
else:
statement(s)3
Eg:
n=int(input(“enter a number:”))
if n>0:
print(“+ve number”)
elif n<0:
print(“negative number”)
else:
print(“Number is 0”)
Indentation:Leading whitespace at beginning of a statement
Nested if statement
It is a programming construct, in which one if statement is enclosed in another if statement.
Syn:
if condition1:
if condition2:
Statement 1
else
Statement 2
2
COMPUTER SCIENCE
Eg:
mark = int(input(“enter your score”))
if mark >= 35 :
print("You passed")
if mark == 100:
print("Perfect!")
else:
print(“needs improvement”)
else:
print(“Fail”)
If the test condition 1 evaluates to true,
test condition 2 is tested. If it is true, Statement 1 is executed
If it is false, Statement set 2 is executed
If the test condition 1 evaluates to false, Control comes out of the if statement
Inner if statement completes its execution , then Outer if statement completes its execution.
Repitition/Iteration
It is a looping construct which provides facility to execute a set of instructions in a program
repetitively, based on a condition
Different Looping constructs in Python are,
➢ while loop
➢ for loop
for Loop
It is a looping statement used to iterate over a range of values or a sequence.
Syn:
for <Control variable> in <Sequence/Items in range> :
Statements
for, in are keywords
Control variable is an identifier
Sequences
• String
• List
• Tuple
Items in range : range() function
Eg: C
for L in “COLLEGE”: O
print(L) L
L
E
G
E
3
COMPUTER SCIENCE
• It is a looping statement used when we know in advance the number of times the loop
will execute
• Statements of the for loop are executed for each value in the range
• Values can be numeric or elements of string, list, tuple
• For every iteration, control variable checks whether each of the value in the range are
traversed. If not control moves to the next iteration. If all the values are traversed, for
loop is terminated.
range() function
It is a builtin function 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
Syn:
range([start,] stop [,step])
Start,step,stop are integer values
Start and step parameters are optional.
by default start=0 and step=1
Eg:
List (range(10)) [0,1,2,3,4,5,6,7,8,9]
List(range(2,10)) [2,3,4,5,6,7,8,9]
List(range(0,30,5)) [0,5,10,15,20,25]
List(range(0,-9,-1)) [0,-1,-2,-3,-4,-5,-6,-7,-8]
for num in range(10,60,10) : 10
print(num) 20
30
40
50
while Loop
It is a looping statement executes a block of code repeatedly as long as the control condition of
the loop is true.
Syn:
while test condition :
Statements
Eg: 1
i=1 2
while i<=5: 3
print (i) 4
5
i=i+1
4
COMPUTER SCIENCE
• Initially the test condition is evaluated.
• If the value of the test condition is true, then the statements in the structure is executed
and control returns to the test condition.
• If the value of the test condition is false, then the while statement is terminated.
• If the value of the test condition is initially false, statements are not executed even once.
Infinite Loop
Loop which is executed infinite number of times without termination
i=10 10
while i>=5: 11
print (i) 12
i = i+ 1 :
:
Programmer should ensure that test condition becomes false after certain iterations,
otherwise loop will become infinite loop(logical error)
Nested loops
If one looping statement is enclosed inside another looping statement then such a sequence of
statements is called as nested loops.
Eg:
for r in range(3):
0 1 2 3
for c in range(4): 0 1 2 3
print(c , end=“ “) 0 1 2 3
print()
Note: any type of loop may be nested within another loop
break : Terminate the innermost loop
for i in range(10): 0
if i==2 : 1
break LOOP ended
print(i)
print(“LOOP ended”)
• It can be used within a while loop or for loop.
• When break is encountered inside any loop, it terminates the current loop and resumes
execution of the statement following that loop.
• It provides a convenient way to terminate the loop if any particular condition is
encountered.
5
COMPUTER SCIENCE
continue :
It bypasses the next statements inside the loop, transfer the control to the next iteration.
for i in range(5):
3
if i<=2 :
4
continue
print(i)
• It can be used within a while loop or for loop.
• It is used to skip some statements of the loop before continuing further in a loop.
Write a program
1. to print the largest number among 2 numbers (using if)
2. to find largest among three numbers (using if)
3. takes the name and age of the user as input and displays a message whether the user
is eligible to apply for a driving license or not(using if else)
4. To find the largest of two numbers (using if else)
5. To print positive difference of two numbers (using if else)
6. To check whether given number is positive or negative (using if else)
7. To check whether given number is odd number or Even number (using if else)
8. To check whether given year is leap year or not (using if else)
9. To find the grade of a student by accepting his percentage of marks(using if elif)
10. To Display appropriate message as per the colour of signal at road crossing(using if elif)
11. To create a simple calculator (for +,-,*,/)(using if elif)
12. That prints minimum and maximum of five numbers entered by the user(Using nested
if)
13. To print the sequence of numbers (using for loop sequence/range)
14. To print even numbers in a sequence(using for loop sequence/range)
15. To count the number of even and odd numbers in the given numbers(using for loop
range)
16. Write a function to print the table of a given number. The number has to be entered
by the user(using range)
17. To print your college name n times (using while loop)
18. To find the sum of digits of an integer number, input by the user(using while loop)
19. To check whether an input number is a palindrome or not(using while loop)
20. WAP to print the pattern(for loop)
12345
1234
123
12
1
6