Conditional Repetition: while Loops
They are similar conditions as when you’re using an
if statement: having a choice of values to enter or continue a loop or checking to see if an input value is outside of a range of specific values.
So, here’s a sample program where the programmer wants to make sure that the user enters a number between
10. And so we have the appropriate input statement and then we do a check. If the number is less than
1 or it’s greater than
10, they haven’t entered an appropriate number and so we’ll give them a second chance.
while loop will continue and the user will continue to be prompted for a number between
10. And if that doesn’t happen, we go back to the beginning of the
while loop, the condition is
True, and repeat it until we eventually get an appropriate number that the program is expecting. Now, what if they never enter an appropriate number? Well, you can create a more complicated loop condition to let that happen, but that’s not the topic for this particular course. But there we can see an example of how to use an
or condition within a
Become a Member to join the conversation.