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

Python While Loop

The Python while loop allows code to be repeatedly executed as long as a condition is true. It checks the condition before running the code block. The while loop is useful when the number of iterations is unknown. It continues in a loop until the condition becomes false. The break statement exits the current loop. The continue statement skips the rest of the current loop iteration and continues with the next.

Uploaded by

Kamalakumar V
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Python While Loop

The Python while loop allows code to be repeatedly executed as long as a condition is true. It checks the condition before running the code block. The while loop is useful when the number of iterations is unknown. It continues in a loop until the condition becomes false. The break statement exits the current loop. The continue statement skips the rest of the current loop iteration and continues with the next.

Uploaded by

Kamalakumar V
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Python While loop

The Python while loop allows a part of the code to be executed until the given condition
returns false. It is also known as a pre-tested loop.

It can be viewed as a repeating if statement. When we don't know the number of iterations
then the while loop is most effective to use.

The syntax is given below.

while expression:

statements
Here, the statements can be a single statement or a group of statements. The expression
should be any valid Python expression resulting in true or false. The true is any non-zero
value and false is 0.

Example-1: Program to print 1 to 10 using while loop

i=1

#The while loop will iterate until condition becomes false.

While(i<=10):

print(i)

i=i+1

Output:

10
Example -2: Program to print table of given numbers.
i=1

number=0

b=9

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

while i<=10:

print("%d X %d = %d \n"%(number,i,number*i))

i = i+1

Output:

Enter the number:10

10 X 1 = 10

10 X 2 = 20

10 X 3 = 30

10 X 4 = 40

10 X 5 = 50

10 X 6 = 60

10 X 7 = 70

10 X 8 = 80

10 X 9 = 90

10 X 10 = 100

Infinite while loop


If the condition is given in the while loop never becomes false, then the while loop will never
terminate, and it turns into the infinite while loop.

Any non-zero value in the while loop indicates an always-true condition, whereas zero


indicates the always-false condition. This type of approach is useful if we want our program
to run continuously in the loop without any disturbance.
Example 1

while (1):

print("Hi! we are inside the infinite while loop")

Output:

Hi! we are inside the infinite while loop

Hi! we are inside the infinite while loop

Example 2

var = 1

while(var != 2):

i = int(input("Enter the number:"))

print("Entered value is %d"%(i))

Output:

Enter the number:10

Entered value is 10

Enter the number:10

Entered value is 10

Enter the number:10

Entered value is 10

Infinite time

Using else with while loop


Python allows us to use the else statement with the while loop also. The else block is
executed when the condition given in the while statement becomes false. Like for loop, if
the while loop is broken using break statement, then the else block will not be executed,
and the statement present after else block will be executed. The else statement is optional
to use with the while loop. Consider the following example.
Example 1
i=1

while(i<=5):

print(i)

i=i+1

else:

print("The while loop exhausted")

Output

The while loop exhausted

Example 2
i=1

while(i<=5):

print(i)

i=i+1

if(i==3):

break

else:

print("The while loop exhausted")

Output:

In the above code, when the break statement encountered, then while loop stopped its
execution and skipped the else statement.
Example-3 Program to print Fibonacci numbers to given limit

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This
means to say the nth term is the sum of (n-1)th and (n-2)th term.

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms

n1, n2 = 0, 1

count = 0

# check if the number of terms is valid

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

print(n1)

nth = n1 + n2

# update values

n1 = n2

n2 = nth

count += 1
Output:

How many terms? 7

Fibonacci sequence:

Python break statement


The break is a keyword in python which is used to bring the program control out of the loop.
The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops. In other words, we can say that break
is used to abort the current execution of the program and the control goes to the next line
after the loop.

The break is commonly used in the cases where we need to break the loop for a given
condition.

The syntax of the break is given below.

#loop statements

break;

Example 1
list =[1,2,3,4]

count = 1;

for i in list:

if i == 4:
print("item matched")

count = count + 1;

break

print("found at",count,"location");

Output:

item matched

found at 2 location

Example 2
str = "python"

for i in str:

if i == 'o':

break

print(i);

Output:

Example 3: break statement with while loop


i = 0;

while 1:

print(i," ",end=""),

i=i+1;

if i == 10:

break;

print("came out of while loop");


Output:

0 1 2 3 4 5 6 7 8 9 came out of while loop

Example 3
n=2

while 1:

i=1;

while i<=10:

print("%d X %d = %d\n"%(n,i,n*i));

i = i+1;

choice = int(input("Do you want to continue printing the table, press 0 for no?"))

if choice == 0:

break;

n=n+1

Output:

2X1=2

2X2=4

2X3=6

2X4=8

2 X 5 = 10

2 X 6 = 12

2 X 7 = 14

2 X 8 = 16

2 X 9 = 18

2 X 10 = 20

Do you want to continue printing the table, press 0 for no?1

3X1=3

3X2=6

3X3=9
3 X 4 = 12

3 X 5 = 15

3 X 6 = 18

3 X 7 = 21

3 X 8 = 24

3 X 9 = 27

3 X 10 = 30

Do you want to continue printing the table, press 0 for no?0

Python continue Statement


The continue statement in Python is used to bring the program control to the beginning of
the loop. The continue statement skips the remaining lines of code inside the loop and start
with the next iteration. It is mainly used for a particular condition inside the loop so that we
can skip some specific code for a particular condition.The continue statement in Python is
used to bring the program control to the beginning of the loop. The continue statement
skips the remaining lines of code inside the loop and start with the next iteration. It is
mainly used for a particular condition inside the loop so that we can skip some specific code
for a particular condition.

Syntax
#loop statements

continue

#the code to be skipped

Consider the following examples.

Example 1
i=0

while(i < 10):

i = i+1

if(i == 5):

continue

print(i)

Output:

1
2

10

Observe the output of above code, the value 5 is skipped because we have provided the if
condition using with continue statement in while loop. When it matched with the given
condition then control transferred to the beginning of the while loop and it skipped the value
5 from the code.

Let's have a look at another example:

Example 2

str = "JavaScript"

for i in str:

if(i == 'S'):

continue

print(i)

Output:

You might also like