Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
627 views

Python While Loop

The Python while loop allows code to be repeatedly executed as long as a condition is true, it can be used when the number of iterations is unknown, and it executes statements or blocks of code as long as its expression remains true, breaking out of the loop if the expression becomes false.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
627 views

Python While Loop

The Python while loop allows code to be repeatedly executed as long as a condition is true, it can be used when the number of iterations is unknown, and it executes statements or blocks of code as long as its expression remains true, breaking out of the loop if the expression becomes false.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

1. while expression:
2. 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.
While loop Flowchart

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


1. i=1
2. #The while loop will iterate until condition becomes false.
3. While(i<=10):
4. print(i)
5. i=i+1

Output:

1
2
3
4
5
6
7
8
9
10

Example -2: Program to print table of given numbers.


1. i=1
2. number=0
3. b=9
4. number = int(input("Enter the number:"))
5. while i<=10:
6. print("%d X %d = %d \n"%(number,i,number*i))
7. 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
1. while (1):
2. 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
1. var = 1
2. while(var != 2):
3. i = int(input("Enter the number:"))
4. 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
1. i=1
2. while(i<=5):
3. print(i)
4. i=i+1
5. else:
6. print("The while loop exhausted")

Example 2
1. i=1
2. while(i<=5):
3. print(i)
4. i=i+1
5. if(i==3):
6. break
7. else:
8. print("The while loop exhausted")

Output:

1
2
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


1. terms = int(input("Enter the terms "))
2. # first two intial terms
3. a = 0
4. b = 1
5. count = 0
6.
7. # check if the number of terms is Zero or negative
8. if (terms <= 0):
9. print("Please enter a valid integer")
10. elif (terms == 1):
11. print("Fibonacci sequence upto",limit,":")
12. print(a)
13. else:
14. print("Fibonacci sequence:")
15. while (count < terms) :
16. print(a, end = ' ')
17. c=a+ b
18. # updateing values
19. a=b
20. b=c
21.
22. count += 1

Output:

Enter the terms 10


Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34

You might also like