0 votes
in Python by
Python while loops

1 Answer

0 votes
by

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

Related questions

0 votes
asked Oct 12, 2021 in Python by rajeshsharma
0 votes
asked Feb 11, 2021 in Python by SakshiSharma
...