Unit 2 Chapter-3 Control Statements New
Unit 2 Chapter-3 Control Statements New
Chapter - 3
Control Statement
Conditional and looping constructs
Introduction
Tools that help in better understanding of the problem
statement and decisions to be made accordingly are:
1. Flow charts
2. Pseudo code
Flow charts
It is the pictorial representation of the sequence of steps
and decisions needed to perform a task.
Flow chart depicts the “flow” of a program.
Commonly used symbols in flow chart
Symbol Name
Start/Stop
Process
Input/Output
Decision
1. Sequence
2. Selection/Decision
3. Iteration/Loop
Program Control Flow
1. Sequence
The statements in the programs are
executed in an order one line at a time
from the top to the bottom of your
program.
2. Selection/Decision
The set of statements are executed based
on the condition and then change the
course of program.
3. Iteration/Loop
Loops are used to repeatedly execute the
same statement(s) in a program.
1. Sequence
2. Selection
(Decision making)
Conditional statements let us write program to do
different tasks or take different paths based on the
outcome of the conditions.
a. if statement
b. if-else statement
c. if-elif-else statement
d. Nested if-else statement
2. Selection (Decision making)
a. if statement
Syntax:
if condition:
statement(s)
e.g.
if x > 0:
print (“x is positive”)
2. Selection (Decision making)
b. if-else statement
e.g.
if age >= 18:
print (“Can Vote”)
else:
print (“Can’t Vote”)
2. Selection (Decision making)
e.g.
#larger number and display the difference.
Example:
num=int(input("Enter number"))
if (num>=0):
if (num==0):
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
2. Selection (Decision making)
e.g. Check whether a number is positive, negative, or
zero.
a) for loop
b) while loop
3. Iteration
a) for loop
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
Note: Body of the loop is indented with respect to the for statement.
copyright NCERT
3. Iteration
The range() Function
Output: 1 2 3 4 5 6 7 8 9 10 Output: 1 3 5 7 9
The function range() is often used in for loops for generating a sequence of
numbers.
Copyright NCERT
Apart from range( ), i (loop control variable) can take
values from string, list, dictionary, etc.
num=[10,20,30,40,50]
for i in num:
print(i)
Nested Loops
e.g.
for i in range(1,3):
print( "Iteration " + i + " of outer loop")
for j in range(1,3): #nested loop Output:
print(j) Iteration 1 of outer loop
1
print("Out of inner loop") 2
print("Out of outer loop") Out of inner loop
Example
a loop to print nos. from 1 to 10
i=1
while (i <=10):
print (i, end=‘ ‘)
i = i+1
while Loop with else
e.g.
x=1
while(x < 3):
print('inside while loop value of x is ',x)
x=x+1
else:
print('inside else value of x is ', x)
Output:
inside while loop value of x is 1
inside while loop value of x is 2
inside else value of x is 3
Nested while loop
Block of statement belonging to while can have
another while statement, i.e. a while can
contain another while.
e.g.
i=1
while(i<=3):
j=1
while(j<=i):
print(j, end=" ") # inner while loop
j=j+1 Output:
print("\n") 1
i=i+1 1 2
1 2 3