The While Loop

The While Loop makes the recursion we used earlier much easier. Think about the While Loop this way:

While it is raining, keep holding the umbrella.

The same scenario in Python would look like:

What we did is, we set the value of "raining" as True (Boolean Operator), to indicate that it is raining. After that, we used a While Loop to iterate/repeat the function "hold the umbrella".

We found the factorial of a number using recursion, so now let's try doing the same with the While Loop.

In this code, we set the initial value of the number as 5 and the value of the factorial as 1.

While number > 0:

So, what's below the While Loop runs only while the number we defined is greater than 0. If it is equal to 0, our whole factorial would be a zero.

If there is no condition, the While Loop will keep running on and on. But what if we had to stop the While Loop when our condition was met? There are 2 ways we can achieve that.

Break command:

Break command in Python is very useful: it stops the While Loop once our condition is met. Let's say we have to take inputs from the user until we get 'quit'. In English, this means "continue the program until the input is quit".