‘While loop’ is the simplest form of loop in Python. But you need to understand it properly. Otherwise it can end up in eating up your memory running the infinity loop. Usually most of the jobs are done by ‘for loop’. But in some special cases, you need to use ‘while loop’. A basic understanding is important.
In plain English we often say – ‘While it is true it keeps on running. While it is not true it stops.’ Logically same thing happens here. While a statement is true, the process is going on. You need a mechanism to stop that process. That is important. Otherwise that statement will eat up your memory.
Consider this code:
b = 1 while b < 50: print(b) b = b + 1
What does it mean? It means, the statement ‘b is less than 50’ is true until the suite or block of code is true inside it. Inside the block we wrote ‘b = b + 1’ and before the beginning of the while loop we defined the value of b as 1.
So in each step b progresses by adding 1 in its value and finishes at 49. In the output you will get 1 to 49.