The while loop is somewhat similar to an if statement, it executes the code inside, if the condition is True. However, as opposed to the if statement, the while loop continues to execute the code repeatedly as long as the condition is True.
The while Loop
The syntax of the while loop is very similar to the if statement, as you can see above. The while loop is not very common, but in some cases, it can be very useful.
Example
Suppose you are numerically calculating a model based on your data. This will typically involve taking the same steps repeatedly until the error between your model and your data is below some boundary. When you can reformulate the problem as repeating an action until a condition is met, a while loop is often the way to go.
In the below example, you initially start with an error equal to 50.0. Next, you will write a while loop, in the condition part, we write error > 1, so that the while loop executes again as long as the error is above 1. Inside the while loop, you divide the error by four and update the error variable. When we run it the first time, we receive 12.5.
Python will then go back to the condition of the while loop with the new error equal to 12.5. The condition will again be true, and the code is executed.
Python will again move back to the condition of the while loop and attempt to re-run the code. Because 3.125 is greater than 1, the code makes the calculation.
Finally, the new condition of the while loop is less than 1, so the code results in an error and won't run.
The while loop is somewhat similar to an
ifstatement, it executes the code inside, if the condition is True. However, as opposed to theifstatement, the while loop continues to execute the code repeatedly as long as the condition is True.The while Loop
The syntax of the while loop is very similar to the if statement, as you can see above. The while loop is not very common, but in some cases, it can be very useful.
Example
Suppose you are numerically calculating a model based on your data. This will typically involve taking the same steps repeatedly until the error between your model and your data is below some boundary. When you can reformulate the problem as
repeating an action until a condition is met, a while loop is often the way to go.In the below example, you initially start with an error equal to
50.0. Next, you will write a while loop, in the condition part, we write error > 1, so that the while loop executes again as long as the error is above 1. Inside the while loop, you divide the error by four and update the error variable. When we run it the first time, we receive 12.5.Python will then go back to the condition of the while loop with the new error equal to 12.5. The condition will again be true, and the code is executed.
Python will again move back to the condition of the while loop and attempt to re-run the code. Because 3.125 is greater than 1, the code makes the calculation.
Finally, the new condition of the while loop is less than 1, so the code results in an error and won't run.