Use While Loop in Python With Control Statements

In this tutorial, learn how to use while loop in Python. You will also learn to use the control statements with the Python while loop.

Perform a simple iteration to print the required numbers using Python. Loop through each element of Python List, Tuple and Dictionary to get print its elements. You can also find the required elements using While loop in Python.

Syntax Of While Loop In Python

Use the while loop with the syntax as given below. The loop requires a single condition to perform iteration over elements. The condition decides how many times the iteration should perform. If you do not give the condition to the while loop, the code will show some error message.

The loop contains the statement to execute when the condition is true. If the condition is false, the statement will not get executed. The statement can be a single line of code or multiple lines of code.

In the statement, you can also put the if condition statement. Inside the if condition, you can put the break or continue control statements to reduce the number of iterations. This can be useful when you want to get the required result from the loop.

The above example showing the syntax of the while loop. You can use the syntax in various types as given in the below examples. If you want to use the if condition inside the loop. You have to use it in the statement variable in the above syntax.

How to use while loop in python with examples

How To Use While Loop With Examples

Let’s take an example to print all single digit numbers. To print numbers from 0 to 9, you have to use the below-given example. It first initializes the variable with 0 and takes condition.i<10 Inside the loop, it contains the statement which prints the number and increments the number by 1.

To increment the variable in Python, you have to use two methods. The first method is to add 1 to the variable to make increment. However, the second method is to put ++ at the end of the variable. The below example showing the first method to make increment to the variable i.

Output

0
1
2
3
4
5
6
7
8
9

The above example showing the numbers from 0 to 9 printed in the output. Each element prints in the single line which means the single element in the single line.

In order to reduce the iteration from the loop in Python. You can use the Python control statements break and continue. The details of these statements with examples are given below.

Include Break Statement

If you want to get the exact single or multiple results from the loop. You may like to use the control statements with the loop. One of the most useful control statement is a break. The break can be used to comes out of the loop when if the condition is true.

The above example prints all the single digit of numbers from 0 to 9. If you wish to print the numbers from 0 to 5 using the above example. You have to put the break statement within the if condition as given in the below example. All these statements should come under the loop.

Output

0
1
2
3
4
5

The above example prints the number from 0 to 5 in the output. It uses the same example as given in the previous example. However, the only difference in the example is the use of the break statement. The break statement performs iteration less than the previous one.

Previously, it performs iteration for 10 times. However, after the use of the break statement, it performs iteration only 6 times. Also, comes out of the loop when it reaches to the break statement within the if condition.

Use Continue Statement

You can use it to comes out of the current iteration and continue with the next iteration. It can be useful when you want to remove the single iteration from the loop.

To use the continue statement, you have to use the control statement within the if condition. See the example below to use the continue statement on your code.

Output

1
2
3
4
6
7
8
9
10

The above example prints all the numbers from 1 to 10 except 5. Because the iteration for 5 using the continue statement. Which gives you result to not to print the number 5 in the output.

You may also like to read

Hope, you like this post of how to use the while loop of Python. If you have any query regarding the tutorial, please comment below.

Also tell me, if you know any other methods I will definitely add it to this post.

References

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.