0% found this document useful (0 votes)
16 views

Unit 2 Chapter-3 Control Statements New

Uploaded by

firegamers419
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)
16 views

Unit 2 Chapter-3 Control Statements New

Uploaded by

firegamers419
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/ 32

Unit 2

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

Connection (Flow Line)


Example of Flow chart
Example of Flow chart
Program Control Flow
Program control flow is the order in which individual
statements or instructions are executed.

The control flow of a program in any


programming language can be broadly
classified into three categories.

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.

There are four types of decision-making statements


in python:

a. if statement
b. if-else statement
c. if-elif-else statement
d. Nested if-else statement
2. Selection (Decision making)
a. if statement

if the condition is True, then the statement written after,


if is executed.

Syntax:
if condition:
statement(s)

e.g.
if x > 0:
print (“x is positive”)
2. Selection (Decision making)
b. if-else statement

if the condition is True, then the statement written after, if is


executed. If False, then statement(s) written after else is
executed
Syntax:
if condition:
statement(s)
else:
statement(s)

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.

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))
if num1 > num2:
diff = num1 - num2
else:
diff = num2 - num1
print("The difference of ",num1, " and
",num2, " is ", diff)
2. Selection (Decision making)
c. if-elif-else statement

It is used for multiple test condition.

In the chained conditions, each condition is checked in


order if previous is False then next is checked, and so on.
If one of them is True then corresponding block of
statement(s) are executed and the statement ends.

If none is true, then else block gets executed if provided.


If more than one condition is true, then only the first true
option block gets executed.
2. Selection (Decision making)
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
e.g.
if per>=90:
print (“A”)
elif per>=70 and per<90:
print (“B”)
elif per>=50 and per<70:
print (“C”)
else:
print (“D”)
2. Selection (Decision making)
d. Nested if-else statement

It is possible to have a condition within another condition.


Such conditions are known as Nested Condition.

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.

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


if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
3. Iteration
Loops are used to repeatedly execute the same
code in a program.

Python provides two types of looping constructs:

a) for loop
b) while loop
3. Iteration
a) for loop

The for statement is used to repeat itself over a range of


values or a sequence, such as a list, string or tuple.

Its Syntax is:

for <control_variable> in <sequence/items in range>:


STATEMENT BLOCK 1
[else: # optional block
STATEMENT BLOCK 2]
e.g.

#Program to print even numbers in a given sequence using 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

The range() is a built-in function in Python.

Syntax of range() function 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 given step value.

If start value is not specified, by default the list starts from 0.


If step is also not specified, by default the value is incremented by 1
in each iteration.

All parameters of range() function must be integers. The step


parameter can be a positive or a negative integer excluding zero.
3. Iteration
e.g. start
stop-1 increment

for i in range(1, 11): for i in range(1, 11,2):


print (i, end=' ') print (i, end=' ')

Output: 1 2 3 4 5 6 7 8 9 10 Output: 1 3 5 7 9

range( ) generates a list of values starting from start till stop-1.

step is optional. if given is added to the value generated, to get


next value in the list.
3. Iteration
i is the variable, which keeps on getting a value generated by
range ( ) function, and the block of statement (s) are worked on
for each value of i.

As the last value is assigned to i, the loop block is executed last


time and control is returned to next statement.

If else is specified in for statement, then next statement


executed will be else.

Result of for statement. range( ) generates a list from 1, 2, 3, 4,


5,6,7,8,9,10 as the step mentioned is 1, i keeps on getting a
value at a time, which is then printed on screen.
3. Iteration
e.g.
#generate sequence of numbers from 0 to 9
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#start value is given as 2


>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]

#step value is 5 and start value is 0


>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]

#step value is -1. Hence, decreasing #sequence is generated


>>> list(range(0, -9, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8]

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.

Example 1: (Values from String)


for i in "Python":
print("Current letter", i)
else:
print("End of loop")

Example 2: (Values from List)

num=[10,20,30,40,50]
for i in num:
print(i)
Nested Loops

A loop may contain another loop inside it. A loop


inside another loop is called a nested loop.

e.g.

Program to demonstrate working of nested for loops.

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

Iteration 2 of outer loop


1
2
Out of inner loop

Copyright NCERT Out of inner loop


Nested for

for num in range(6):


for i in range(num):
print (num, end=" ")
print("\n")
Output:
1
1 2
1 2 3
1 2 3 4 5
1 2 3 4 5
3. Iteration
b) while loop

The statement with keyword while followed by boolean


condition followed by colon (:).
What follows next is block of statement(s).

The statement(s) in BLOCK 1 keeps on executing till


condition in while remains True;
once the condition becomes False and if the else clause is
written in while, then else will get executed.

While loop may not execute even once, if the condition


evaluates to false, initially, as the condition is tested before
entering the loop.
3. Iteration
Syntax:
while condition: # condition is Boolean expression
returning True or False
STATEMENTs
[else: # optional part of while
STATEMENTs ]

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

You might also like