In programming languages, loops are a set of instructions that execute a sequence of code continuously until a certain condition is fulfilled. Most modern programming languages do include the concept of loops. The syntax for loops in each language may differ but the logic being used remains the same. Many programming languages have several types of loops and the most renowned ones are while and for loop. Today we will only learn about while loop and where it should be preferred over other kinds of loops. In most cases loops are interchangeable with each other but while loop should be preferred over other loops when the required condition is boolean. We can think of a while loop as a repeating if statement, to make the concept easier to understand.

Syntax

We should first discuss the syntax of a while loop in python before going any further. Following is the syntax while loop uses in python programing language:

Using while Loop:

Let’s take a programmer as an example who has to make a program that outputs a sequence of numbers. Let’s say the sequence is from one to ten. The programmer will have to write ten or more lines of code. The bigger the sequence gets, the harder it gets for the programmer to write the code. He/She will have to write a similar line of code repeatedly. Writing code in such a way becomes inefficient and wastes a lot of time. This is where we can use loops, an efficient tool, to concise hundreds of lines of code into just three-four lines. To make the above-mentioned program now we will use the while loop. The following script will output a sequence of numbers on screen:

Output:

In this simple program, we have concised 10 lines of code into just four lines. We can use the same code to output any sequence of numbers by just changing the condition. Now let’s discuss how the code given above is actually working. First, we declared a variable named “number” and give it a value “1”. On the second line, we used a loop and gave it a condition i.e. less than or equal to ten. In the first iteration, the “number” will be “1” and the condition “number <= 10” of the While loop will be true. As a result, the body of the While loop will get executed. The function “print(number)” prints whatever the value of the “number” variable. Then the next line of code increments the value of “number”. This process is repeated again and again until the value of “number” becomes “11” and the loop is terminated because the condition is now false. If we do not increment the value of “number”, the condition will never become false and the loop will run forever.

Break Statement

We can combine while loops with other statements to perform certain actions. The break statement comes in handy to exit a loop if a specified condition is met. For example, let’s say we want to exit the loop in the above-mentioned program when the value of the variable “number” reaches “5”. We can use the break statement to perform this action:

Output:

Continue Statement

This statement is somewhat similar to the break statement. However, instead of terminating the loop, the loop jumps to the next iteration and skips the current iteration. If we want to skip five in the sequence of numbers, we can use the continue statement in the following way:

Output:

As you can see in the screenshot above when the variable “number” reached the value of “5” the conditional statement became true and the continue statement was executed. The execution of the current iteration stopped due to the continue statement; hence the value of “number” i.e. “5” was not printed out.

Else Statement

We can use the else statement to execute a block of code once the original condition becomes false:

Output:

Conclusion

While loop like other loops is used to repeat a block of code over and over again. Loops are very basic yet one of the most effective tools in any programming language. In this write-up, we have learned what a While loop is and how to use it in our daily programming tasks. Moreover, we learned how to combine the while loop with other statements to perform certain actions.

Python while Loop with Examples   TecAdmin - 92Python while Loop with Examples   TecAdmin - 73Python while Loop with Examples   TecAdmin - 53Python while Loop with Examples   TecAdmin - 1