3 Flow of Control (While)
3 Flow of Control (While)
While Loop
The ‘While’ Loop
The while statement executes a block of code repeatedly as long as
the control condition of the loop is true. The control condition of
the while loop is executed before any statement inside the loop is
executed. After each iteration, the control condition is tested
again and the loop continues as long as the condition remains
true. When this condition becomes false, the statements in the
body of loop are not executed and the control is transferred to the
statement immediately following the body of while loop. If the
condition of the while loop is initially false, the body is not
executed even once.
The statements within the body of the while loop must ensure that
the condition eventually becomes false; otherwise the loop will
become an infinite loop, leading to a logical error in the program.
Syntax of while Loop
while test_condition:
body of while
Program to print first 5 natural numbers
using while loop.
"""
num1=int(input("Enter the number fow which
you find the factor"))
for n in range(1,num1):
if(num1%n==0):
print(n, end=' ')
print(num1)
"""
num1=int(input("Enter the number fow which
you find the factor"))
n=1
while(n<=num1):
if(num1%n==0):
print(n, end=' ')
n+=1
No. is prime or not
num1=int(input("enter"))
for n in range(num1-1,1,-1):
if(num1%n!=0):
if(n==1):
print(num1,"is prime")
continue
else:
print(num1," is not a prime number")
break
print(num1,"is prime")
#Find the factors of a number using while loop
num = int(input("Enter a number to find its factor: "))
print (1, end=' ')
factor = 2
while factor <= num :
if num % factor == 0:
print(factor, end=' ‘)
factor += 1
Break and Continue Statement
Looping constructs allow programmers to repeat tasks
efficiently. In certain situations, when some particular
condition occurs, we may want to exit from a loop (come out
of the loop forever) or skip some statements of the loop before
continuing further in the loop. These requirements can be
achieved by using break and continue statements, respectively.
Python provides these statements as a tool to give more
flexibility to the programmer to control the flow of execution
The
of a"while true" loop in python runs without any conditions until
program.
the break statement executes inside the loop. To run a statement
if a python while loop fails, the programmer can implement a python
"while" with else loop.
Break Statement
The break statement alters the normal flow of execution as it
terminates the current loop and resumes execution of the
statement following that loop.
#accept the numbers from the user, if a user enters a negative
number stop accepting the number.
while True:
n = int(input("Enter different numbers"))
if n<0 :
break
Find the sum of all the positive numbers entered by the user. As soon
as the user enters a negative number, stop taking in any further input
from the user and display the sum .
n=int(input("Enter the number to find sum"))
sum=0
ch='y'
while (ch=='y' or ch=='Y'):
if (n>=0):
sum+=n
else:
print("Negative number entered....")
break
ch=input("Do you wish to enter more numbers y/n.")
if (ch=='y' or ch=='Y'):
n=int(input("Enter the number to find sum"))
else:
break
print("sum of all the positive numbers entered by you is: ", sum)
Program to check if the input number is prime or not.
Continue Statement
When a continue statement is encountered, the control skips the
execution of remaining statements inside the body of the loop for
the current iteration and jumps to the beginning of the loop for the
next iteration. If the loop’s condition is still true, the loop is entered
again, else the control is transferred to the statement immediately
Program to demonstrate the use of continue statement.
num = 0
for num in range(6):
num = num + 1
if num == 3:
continue
print('Num has value ' + str(num))
print('End of loop')
Nested Loops A loop may contain another loop
inside it. A loop inside another loop
is called a nested loop.
table=1
while(table<=5): Print tables from 1 to 5
multiple=1
while (multiple<=10):
print(table* multiple)
multiple+=1
print()
table+=1
Print tables from 1 to 5
for n in range(1,6):
print("\nTable of",n,"\n")
for i in range(1,11):
print(n,"X",i,"=",n*i)
Python does not impose any restriction on how many loops
can be nested inside a loop or on the levels of nesting. Any
type of loop (for/while) may be nested within another loop
(for/while).
Program to print the pattern for a number input by the user
The output pattern to be generated is
1
12
123
1234
12345
for i in range(0,4):
num=int(input("Enter Number "))
if num==0:
smallest,largest=num,num
elif(num>largest):
largest=num
elif(num<smallest):
smallest=num