Python While Loop – How to Use WHILE Loop in Python


How to Use the While Loop Statement in Python

In this tutorial, you'll learn the WHILE loop statement with examples in Python. A WHILE loop is a control flow statement that allows you to execute a block of code repeatedly as long as a given condition remains true.

Python WHILE Loop Syntax

The syntax of a WHILE loop statement is as follows:

while(expression):
statement_list

Here, the statement_list can be a single statement or a block of statements. While a condition remains true, the loop iterates and the program executes the statement_list.

Remarks

  • Use the break statement if you want to exit the loop.
  • Use the continue statement if you want to skip and continue the next iteration.

Examples

Iterate While a Condition is True

The following code prints the value of x as long as x is less than 4:

x = 0
while(x < 4):
print (x)
x += 1

Make sure to increment x, or the loop will continue forever.

Output:

0
1
2
3

The following code prints the value of x as long as x is greater than or equal to 0:

x = 5
while(x >= 0):
print (x)
x -= 1

Make sure to decrement x, or the loop will not stop.

Output:

5
4
3
2
1
0

Continue Statement and WHILE Loop

You can stop the current iteration and continue with the next iteration using the IF and continue statements.

The following code continues to the next iteration when x is 4:

x = 5
while(x >= 0):
if(x == 4):
x -= 1
continue
print (x)
x -= 1

Output:

5
3
2
1
0

Using the Break Statement to Exit the Loop

You can exit the loop even if the condition remains true using the IF and break statements.

The following code exits the loop when x is 2:

x = 5
while(x >= 0):
if(x == 2):
break
print (x)
x -= 1

Output:

5
4
3

Find the Maximum Value in a List

The following code illustrates an example of how to find the maximum value in a list of numbers:

list = [2, 3, 4, 5, 10, 2, 20, 9, 21]
count = len(list)
x = 0;
max = 0
while(x < count):
if(list[x] > max):
max = list[x]
x += 1
print ("The maximum value is", str(max) + ".")

Output:

The maximum value is 21.

Find the Minimum Value in a List

The following code is an example of how to find the smallest number:

list = [10, 2, 3, 4, 5, 10, 2, 20, 9, 21]
count = len(list)
min = list[0]
while(count > 0):
count -= 1
if(list[count] < min):
min = list[count]
print ("The minimum value is", str(min) + ".")

Output:

The minimum value is 2.

Sum Values in a List

The following code illustrates an example of how to sum all numbers in a list:

list = [100, 20, 13, 24, 57, 109, 22, 20, 90, 120]
sum = 0;
count = len(list)
while(count > 0):
sum += list[count-1]
count -= 1
print ("The total value is", str(sum) + ".")

Output:

The total value is 575.

In this tutorial, you've learned how to use the WHILE loop statement with examples in Python. A WHILE loop iterates over a block of code while a condition remains true. To exit the WHILE loop, you can use the break statement. If you want to skit the current iteration and continue the next iteration, use the continue statement. Instead of the WHILE loop, use the FOR loop statement if you know the number of times you want to loop through a block of code.

Leave a Reply

Your email address will not be published. Required fields are marked *