Controlling Your While Loops
Flag Based While Loop
To control when to exit a while loop, we can also a flag like variable to make the condition become False based on user input.
This type of formatting a while loop help us to potentially loop forever, but at the end of the code block we get to choose if we want to iterate again.
At the bottom of the while loop, we have an input that asks the user if they want to exit
If they do want to exit, our looping condition would turn to
false
and end the loopOtherwise; the loop continues to execute its code block
While Loop and Numbers
We can manipulate variables to represent a range of numbers by repetitively applying arithmetic operators.
Assignment Operators
These operators will manipulate/update the left variable with the result of the arithmetic operation with the right operand.
If we can use these inside a while loop code block, we can repetitively change the variable to make a condition become False to end the while loop.
Code Examples:
Last updated