Categories

Dec 12, 2019 in Python
Q: Python while loops

1 Answer

Dec 12, 2019

The while loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed as long as the given condition is true.

It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance.

The syntax is given below.

  1. while expression:  
  2.     statements  

Here, the statements can be a single statement or the group of statements. The expression should be any valid python expression resulting into true or false. The true is any non-zero value.

Python while loop

Example 1

  1. i=1;  
  2. while i<=10:  
  3.     print(i);  
  4.     i=i+1;  

Output:

1
2
3
4
5
6
7
8
9
10
Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Nov 24 in Python
Aug 30 in Python
Aug 30 in Python
...