0% found this document useful (0 votes)
7 views33 pages

Chapter-7 PYTHON CONTROL

Chapter 7 discusses control structures in Python, including sequential, selection, and iterative statements. It provides examples of using if statements for decision making, as well as loops for repetitive tasks. Additionally, it covers the use of break and continue statements to manage loop execution.
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)
7 views33 pages

Chapter-7 PYTHON CONTROL

Chapter 7 discusses control structures in Python, including sequential, selection, and iterative statements. It provides examples of using if statements for decision making, as well as loops for repetitive tasks. Additionally, it covers the use of break and continue statements to manage loop execution.
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/ 33

Chapter-7

CONTROL STRUCTURES IN
PYTHON
>>12+567 '45'
579 >>p+10
>>a,b=34,78 TypeError:
c=a+b >>s=int(input())
>>print(c) 23
112 >>s
>>p=input() 23
45 s+10
>>p 33
#WAP to accept 2 numbers and add them

a=int(input('Enter a number'))
b=int(input('Enter another number'))
sum=a+b
print('The sum is',sum)
#WAP to accept 2 numbers and find their
quotient

a=int(input('Enter a number'))
b=int(input('Enter another number'))
div=a/b
print('The quotient is',div)
#WAP to accept 3 numbers and find their
average

a=int(input('Enter the first number'))


b=int(input('Enter the second number'))
c=int(input('Enter the third number'))
avg=(a+b+c)/3
print('The average of',a ,b,c,'is',avg)
CONTROL STRUCTURES

A control structure is a programming language construct


which affects the flow of the execution of a program

CONTROL
STRUCTURES

SEQUENTIAL SELECTION ITERATIVE


STATEMENTS STATEMENTS STATEMENTS
SELECTION STATEMENTS

The if statement is a decision making statement


which is used to control the flow of execution of
statements.
It contains a conditional expression
for eg (if age>=18) using which data is compared
#WAP to accept age and print eligibility to vote

age=int(input('Enter your age:'))


if age>=18:
print('You can vote')
else:
print('You are not eligible to vote')
#WAP to accept a number and print whether
it is a positive or a negative number

num1=int(input('Enter a number:'))
if num1>0:
print(num1,'is a positive number')
else:
print(num1,'is a negative number')
#WAP to accept a number and print whether
it an odd or an even number

num=int(input('Enter a number:'))
if num%2==0:
print(num,'is an even number')
else:
print(num,'is an odd number')
#WAP to accept marks and print grade
marks=int(input('Enter a number:'))
if marks>=90:
print('You have secured A grade')
elif marks>=80:
print('You have secured B grade')
elif marks>=70:
print('You have secured C grade')
elif marks>=60:
print('You have secured C grade')
else:
print('You have secured D grade')
ITERATIVE STATEMENTS

Iterative Statements refers to the statements that are used


to repeat a task based on a given condition
These statements are also called as looping statements
#WAP to print first 10 natural numbers
x=1
while x<=10:
print(x)
x=x+1
#WAP to print even number between 11-20
x=12
while x<=20:
print(x)
x=x+2
#WAP to print

for s in range(4,34,5):
print(s)

#WAP to print the foll series 3.5,5,6.5.......11


x=3.5
while x<12:
print(x)
x=x+1.5
The range function

We can generate a list of numbers ranging from a starting


value to the number just before the ending number
for s in range(7,40,4):
print(s)
The break statement

 A break statement in Python is used to terminate the current loop


prematurely when it's encountered. It immediately stops the iterations
#use of break statement
for i in range(10):
print(i)
if i==8:
break
The Continue Statement

 The continue keyword is used to end the current iteration in a for


loop (or a while loop), and continues to the next iteration.
#use of continue statement
for i in range(10):
if i==4:
continue
print(i)
Chapter-7
Control Structures in Python
A control structure is a programming construct which
affects the flow of the execution of a program

Control Structure

Sequential Selection Iterative


statements statements statements
Sequential Statements:

These statements are


executed in sequential
order ,ie. One .after the
other without jumps.

A sequential structure is
also known as a straight
line path.
Sequential Statements
# Write a program to add two numbers.

Output:
Selection statements : These are also
known as branching statements or conditional
or decision-making statements. These
are used to select part of a program to be
executed based on the condition.

Python provides the following selection


statements:

1. if statement- if statement executes a


block of code only when the specified
condition is true.
Syntax
if condition:
# body of if statement
2. if-else statement-if the condition in the if statement evaluates
to False , the else statement executes.

Syntax Example: Write a program to find a positive or


if condition: negative number.
# body of if
statement number = int(input('Enter a number: '))
else: if number>0:
# body of print('Positive number')
else statement else: Output:
print(‘Negative number’) Enter a number: 10
Positive number
3.if-elif-else statement-
However, if we need to make a choice between more than two
alternatives, we use the if...elif...else statement.

Syntax # Write a program to get a number is positive or


if condition1: negative or zero
# code block 1
elif condition2: number = int(input('Enter a number: '))
# code block 2 if number > 0:
else: print('Positive number')
# code block 3 elif number < 0:
print('Negative number')
Output
else: Enter a number: -5
print('Zero’) Negative number
# Write a program a find greater number.
# Write a program to cast your vote.

DO YOUR SELF

#WAP to grade a student according to his/her marks


Iterative Statements refer to
the statements that are used
to repeat a task based on a
given condition. These
statements are also known as
looping statements.

Python provides the following


iterative statements:
i) For loop
ii) While loop
For loop-The for loop allows the user to repeat a set of
statements a certain number of times.
The range function-The range() function returns a sequence of numbers, starting
with increments by 1 and ends at a specified number.

Example:
# Write a program to print table of 5
While loop
While Loop In Python is used to execute a block of statement till the given condition
is true. And when the condition is false, the control will come out of the loop.

Syntax

while (<condition>): Program 1 Program 2


statements
JUMP STATEMENTS

The break Statement The continue Statement


With the break statement we can stop the With the continue statement we can stop the current
loop before it has looped through all the iteration of the loop, and continue with the next:
items.

#Exit the loop when x is "banana": # Do not print banana:


fruits=("apple", "banana", "cherry“)
fruits=("apple", "banana", "cherry“) for x in fruits:
for x in fruits: if x == "banana":
print(x) continue
if x == "banana": print(x)
break

Output: Output:
apple apple
banana cherry
JUMP STATEMENTS

The break Statement The continue Statement


for x in range(1,10):
for x in range(1,10):
print (x) if x==8:
if x==8: continue
break
print (x)

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

You might also like