0% found this document useful (0 votes)
6 views22 pages

CHAP-4 Flow of Control

Uploaded by

kuldeep kaur
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)
6 views22 pages

CHAP-4 Flow of Control

Uploaded by

kuldeep kaur
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/ 22

CHAPTER – 4 Flow of Control

A program's control flow is the order in which the program's code executes.

Control Flow

This type of statements execute one after the other


Sequential
without any jump

Program uses a condition to decide which part of the


Flow Control Conditional
program will be executed

A set of statement is executed by multiple no of time


Iteration
in a loop
Conditional/Selection Statement- These are features of a programming
language, which perform different computations or actions depending on whether a
programmer-specified boolean condition evaluates to true or false.

Conditional Statement

if if-else if-elif nested if-else

if statement:
➢ "if statement" is written by using the “if” keyword.
➢ It states if condition given in program is true, perform all steps that
follow.
➢ And if condition given in program is false, it won’t perform at all
➢ Python relies on indentation (whitespace at the beginning of
a line) to define scope in the code.
Syntax:

if condition:

block of if

Examples:

1)

2)
#ELIGIBLE TO VOTE OR NOT (with only if )
age=int(input("Enter your age:::"))
if age>=18:
print("YOU ARE ELIGIBLE To VOTE..")
if age<18:
print("YOU ARE NOT ELIGIBLE To VOTE..")

if-else statement:
➢ "if-else statement" is written by using the “if” and “else” keyword.
➢ In case if condition is evaluated to true, then the
statement/statements indented inside if statement are
executed, otherwise statement/statements following else
statement are executed

Syntax:

if condition:

block of if

else:

block of else

Examples:

1)

2)
#ELIGIBLE TO VOTE OR NOT (with if-else)
age = int(input("Enter your age:::"))
if age>=18:
print("YOU ARE ELIGIBLE To VOTE..")
else:
print("YOU ARE NOT ELIGIBLE To VOTE..")

PROGRAMMING PRACTICE OF ONLY Ifs and if-else blocks


1.
#Positive and Negative Number Checker (with only if)
N=int(input("Enter any no :::"))
if N>=0:
print("POSITIVE NUMBER ENTERED....")
if N<0:
print("NEGATIVE NUMBER ENTERED....")

2.
#Positive and Negative Number Checker (with if-else)
N=int(input("Enter any no :::"))
if N>=0:
print("POSITIVE NUMBER ENTERED....")
else:
print("NEGATIVE NUMBER ENTERED....")

3.
#Even and Odd Number Checker (with only if)
N=int(input("Enter any no :::"))

if N%2==0:
print("YOU HAVE ENTERED EVEN NUMBER....")
if N%2!=0:
print("YOU HAVE ENTERED ODD NUMBER....")

4.
#Even and Odd Number Checker (with if-else)
N=int(input("Enter any no :::"))
if N%2==0:
print("YOU HAVE ENTERED EVEN NUMBER....")
else:
print("YOU HAVE ENTERED ODD NUMBER....")

5.
# Python Program to check student is “pass” or “fail” (with only if)
marks=int(input("Enter marks :::"))
if marks>=33:
print("YOU HAVE PASSED....")
if marks<33:
print("YOU HAVE FAILED....")

6.
# Python Program to check student is “pass” or “fail” (with if-else)
marks=int(input("Enter marks :::"))
if marks>=33:
print("YOU HAVE PASSED....")
else:
print("YOU HAVE FAILED....")

7.
#program to check whether the entered year is LEAP YEAR or Not
yr= int(input("Enter any four digit Year::::"))
if yr%4==0:
print("Leap Year!!!!")
else:
print("Not a Leap Year!!!!")

if – elif - else statement: This statement is used to check with multiple options. As soon as
one condition is ‘true’ then rest of the conditions by passed.
Syntax:
if condition:
block of if statements
elif condition:
block of statements
elif condition:
block of statements
else:
block of else
Examples:
QUES. Write a program to create a simple calculator performing only four basic operations.

#Program to create a four function calculator


result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result = val1/val2
else:
print("Wrong input, program terminated")
print("The result is ",result)

Output:
Enter value 1: 84
Enter value 2: 4
Enter any one of the operator (+,-,*,/): /
The result is 21.0

Nested if-else statement: To check for multiple test expressions and execute different codes
for more than 2 conditions. Any number of these statements can be nested inside one another.
Indent is the only way to figure out the level of nesting.
Syntax:
if condition:
if condition:
block of if
else:
block of else
else:
if condition:
block of if
else:
block of else

Examples:

PROGRAMMING PRACTICE

Ques1. Write the Python program to print the largest between three integers.
Ans.
Ques2. Write a Python program to print the grade of student on the basis of
marks entered by the user. The slab is as follows:
Marks Grade
90 and above A+
>=75 and <90 A
>=50 and <75 B
>=35 and <50 C
Below 35 D
Ans.
**********Output of the program ***************

Ques3. Write a Python program to accept the salary then calculate and print
the tax amount. The slab is as follows:
Salary Tax %age
Upto 50000 5%
>50000 and <=60000 7%
>60000 and <=70000 8%
Above 70000 10%

Ans.
Ques4. Write a Python program to accept three integers from the user then
sort these numbers in ascending order.
Ans.

Ques5. Write the Python program to print the largest as well as smallest
between three integers.
Ans.
Looping / Iteration Statement
Looping / Iteration

Loop statements allows us to execute a statement/s multiple


times, based on a condition

Each time body of loop is executed, it is called as an iteration

Looping/Iteration Statement

for-loop while-loop

range()
• In loops, range() is used to control how many times the loop will be
repeated.
• When working with range(), you can pass between 1 and 3 integer
arguments to it:
start states the integer value at which the sequence begins, if this is not
included then start begins at 0

stop is always required and is the integer that is counted up to but not included

step sets how much to increase (or decrease in the case of negative
numbers) the next iteration, if this is omitted then step defaults to 1

Syntax:
range ( [ start ] , stop ,[ step ] )

command Output
range(10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1,10) [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1,11) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(2, 21, 2) [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
range(5, 51, 5) [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
range(0, -11, -1) [ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]

for Loop - The for statement is used to iterate over a range of values or a sequence. The for
loop is executed for each of the items in the range. These values can be either numeric or they
can be elements of a data type like a string, list, or tuple.

Syntax:
for <control-variable> in <sequence/items in range>:
statements inside body of the loop
Eg:
for i in “Rajan”:
print(i)
R
a
j
a
n
PROGRAMMING PRACTICE:-

#Program to print “Hello World” message on output screen for 100 times
for i in range(100):
print(“Hello World”)
#Program to print first 50 Whole Numbers like( 0……49)Every number should be printed in new
line…
for i in range(50):
print(i)

#Program to print first 50 Natural Numbers


for i in range(1,51):
print(i)

#Program to print only odd numbers in first 10 Natural Numbers


for j in range(1,11,2):
print(“j=”, j)
OUTPUT:
j= 1
j= 3
j= 5
j= 7
j= 9

# Python program to print numbers till 10(0-9) and there sum in end
sum=0
for j in range(10):
print("j=",j)
sum=sum+j
print("sum=",sum)
OUTPUT:
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
j= 9
sum= 45

The while Loop - With the while loop we can execute a set of statements as long as a
condition is true.
Syntax
while condition/expression:
statement(s)
Here, statement(s) may be a single statement or a block of statements. The condition may be
any expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following
the loop.

Eg;

#Print multiples of 10 for numbers in a given range


num=1
while num <5:
print(num * 10)
num+=1
OUTPUT:
10
20
30
40

# Python program to print numbers till 5(0-4) and there sum in end
sum=0
j=0
while j<5:
print("j=",j)
sum = sum+j
j=j+1
print("sum=",sum)
OUTPUT:
j= 0
j= 1
j= 2
j= 3
j= 4
sum= 10
for loop while loop
It is called ‘definite loop’ It is called ‘indefinite loop’
It is known in advance how many times the loop is Number of iterations are not exactly known
going to execute.
In 'for' loop iteration statement is written at top, In 'while' loop, the iteration statement can be
hence, executes only after all statements in loop are written anywhere in the loop.
executed.

Jump statements / Loop control statements

break pass

Break statement
• Terminates the loop containing it and resumes the statement just
after the end of that loop
• If break statement appears in nested loop, then break will terminate the
innermost
loop. (loop within ‘break’ statement written)
Continue statement
• It is used to skip rest of statements in loop for current iteration
• If continue statement encounters in loop, control jumps to the
beginning of the loop for next iteration (skipping statements
inside loop) without terminating the loop
Pass statement
• It is used when a statement is required syntactically but you do not
want any command or code to execute.
• The pass statement is a null operation; nothing happens when it
executes. The pass is also useful in places where your code will
eventually go, but has not been written yet.

For loop - with break Statement


With the break statement we can stop the loop before it has looped
through all the items:

Output:

2)

Output:

For loop - with continue Statement


1.
Output:::

For loop - with pass Statement

The Infinite Loop


A loop becomes infinite loop if a condition never becomes FALSE. You
must use caution when using while loops because of the possibility that
this condition never resolves to a FALSE value. This results in a loop that
never ends. Such a loop is called an infinite loop.
Using else Statement with Loops
Python supports to have an else statement associated with a loop statement.
• If the else statement is used with a for loop, the else statement is
executed when the loop has exhausted iterating the list.
• If the else statement is used with a while loop, the else statement is
executed when the condition becomes false.

OUTPUT:

Nested Loop (Loops Inside Loops):


• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time for each iteration of the
"outer loop":
Output:

2)
#Demonstration of nested loops
for r in range(3):
print("I've entered in Outer loop block")

for c in range(3):
print("I've entered in Inner loop block")
print("r=",r,"c=",c)

print("Again control came back in Outer loop block")


print()
print("OUT OF Both LOOP BLOCKS")

3)
#Demonstration of nested loops
for r in range(5):
for c in range(5):
print(" * ", end="")
print()
OUTPUT:-

Programming Practice
1. Write a python program for printing FIBONNACCI SERIES…
Hint :- 0 1 1 2 3 5 8 13 21………nth

2. Write a python program for printing the table of any number.

Output of the Program:


3. Write a python program for printing the reverse of an entered digit number.

Output::

4. Write a python program for checking entered number is Palindrome or not.


5. Write a python program for checking entered number is Armstrong or not.

6. Write a python program for checking entered number is Prime or not.

7. Write a python program for printing the sum of an entered digit number.

You might also like