Programs

While Loop in Python [With Syntax and Examples]

One of the popular high-level programming languages “python” has been at the forefront for its ease of use and wide applications. Python is one of the most popular programming languages for developers. Applications of python include system scripting, development of software, web development, etc. So, if you are a Python beginner, the best thing you can do is work on some real-time Python project ideas.

There are several reasons why python is chosen over other languages.

  • It can be widely used in different platforms like Windows, Linux, etc.
  • The syntax of the python statements is simple.
  •  Developers are able to write codes consisting of fewer lines.
  • Execution of a program takes place once it is written.

In this article, we will be focusing on an important concept of python, i.e. the working of the while loop.

Loops 

The programming statements follow a sequence of execution steps. However, there might be certain conditions when instead of following the next statement, the program has to follow the previous codes. This is where loop comes to play where repeated writing of the codes is not required. Specifically mentioning the segment of codes within a loop, the program comes to know that the following part has to be executed again. The process is iterative unless and until a stop signal is reached. Several types of loops are offered by the python programming language for repeating the block of codes several times. 

Python loops

Python loops are mostly used as it simplifies the process of writing codes. If a block of statements has to run for ten times, then writing the exact code for ten times, the whole process can be simplified in a few statements for a finite number of times. 

Python loops include the python for loop, while loop in python, and the python do-while and the. In the following article the latter two concepts will be discussed for a better understanding of the use of loops.

Python while loop

The while loop is used for the repeated execution of a set of statements until a specific condition is met. A condition has to be specified in using the while loop. Once the condition becomes false, the iterative process stops, and the next line of code is executed.  

In conditions where the number of iterations is not known, the while loop can be used.

The python while loop may also be termed as a pre-tested loop. Without a condition, the loop will be executed infinitely as there are no specific times mentioned.

Syntax of a while loop 

while condition:

     statements

Example of while loop in python

  • A simple while loop

Input:

i = 1

while i < 4:

print(i)

i += 1

Output: 1

2

3

  • Using the break statement: With the use of the break statement, the execution of the loop can be stopped even when the while condition is true.
  • Example: 

Input: i = 1

while i < 4:

  print(i)

  if (i == 3):

     break

  i += 1

Output: 1

2

3

  • Using the continue statement: Using the continue statement can stop the iteration process and continue with the next step
  • Example: 

Input: i = 1

while i < 5:

i += 1

  if i == 4:

    continue

  print(i)

Output: 2

3

5

  • Using the else statement: The while loop in python is used for the execution of statements when the given condition is true. The else block of statement is executed only when the condition of while becomes false. Even if the user is using the break statement, the else block won’t work, as it breaks out of the loop, but the whole condition is still true. Therefore, until and unless the condition of while becomes false, the else statement cannot be executed.

Syntax of a python while loop

while condition:

     # statements

else:

      # statements

  • Example: 

Input: i = 2

while i < 5:

  print(i)

   i += 1

else:

   print(“while condition is not satisfied”)

Output: 2

3

4

while condition is not satisfied

Python do-while loop

The do-while loop in python is also known as the post-tested loop. In this situation, the condition is checked only after the execution of the code. Python doesn’t contain the do-while loop, but the code can be written to emulate the do-while condition. 

The difference between the do-while loop and that of the while loop is that in the case of the while loop, the statements might not even be executed once if the required condition is not met. However, in the do-while loop, the loop will run once, only then the condition will be checked.

The syntax of a python do-while loop is shown below

Figure1: The general syntax of a python do-while loop

Source

Syntax: do {  

     #statement  

} while (condition);  

Termination of a do-while loop occurs when the condition of the loop turns out to be false or upon the execution of a break statement.

Example

  • Input: i = 1  

while True:  

print(i)  

i = i + 1  

if(i > 5):  

break

Conclusion

The while loop in python and the python do-while loop are important concepts of looping in python programming. Understanding the concepts is crucial as they will lead to the building up of complex programs to solve real day problems. Python being the vital part of machine learning, artificial intelligence, data analyst, any person dreaming to become experts in these fields have to grasp the knowledge at an early step.

The Executive Programme in Data Science provided by upGrad trains all those who are highly interested in learning more of the python programming language. If you are working professionals (both male and female) within the age group of 21-45, then here’s the chance for you to get trained by industry experts. The upGrad’s course certified by IIIT-Bangalore provides a platform directing you towards your aim of becoming experts in the respective field. Feel free to drop off any queries related to the course. Our team will be happy to assist you.

What is a do-while loop in Python?<br />

In Python, the do-while loop is also referred to as post-tested loop. The condition is only tested after the code has been executed in this case. The do-while loop is not available in Python, but the code can be created to simulate it.
The while loop differs from the do-while loop in that the statements in the while loop may not even be performed once if the required condition is not satisfied. The do-while loop, on the other hand, will execute once and then verify the condition.
The syntax of a do-while loop in general is -
do{
Loop block
} while(condition);

Can we use else block with a while loop in Python?

While loops, like for loops, can have an additional else block. When the predicate in the while loop condition becomes False, the else portion is executed.
A break statement can be used to end the while loop. The else portion is ignored in such circumstances. As a result, if no break occurs as well as the condition is false, the else part of a while loop executes.

What are the differences between a while and for loop in Python?

- While iteration in the for loop is running, conditional checking, initialization, as well as increment/decrement are all done. In the syntax, however, just initialization and condition checking are possible.
- When we know the number of iterations at the time of execution, we employ a for loop. On the other hand, with a while loop, we can run it even if we don't know how many iterations there are.
- It will repeat the loop infinitely if you forget to place the conditional statement in the for loop, but it will show you an error if you forget to put the conditional expression in the while loop.
- The for loop's syntax will be run only if the initialization statement is at the top of the syntax, whereas the while loop's syntax will be executed regardless of where the initialization statement is located.

Prepare for a Career of the Future

0 replies on “While Loop in Python [With Syntax and Examples]”

Accelerate Your Career with upGrad

Our Popular Data Science Course

×