While Loops and Flags
When writing programs that don't necessarily deal with numbers, you may use a flag based loop to help you do repetitions.
There are some problems that the end point of the task is unclear. In such scenario, it is helpful to write a potential infinite loop and construct an exit mechanism to get out of it.
Example of a flag
Please note that it does not need to be called a flag, but it is a representation of a flag being either being true or false.
Initialization: You start by setting a variable called
flag
totrue
.Condition Check: You have a loop that will continue to run as long as
flag
istrue
.Loop Execution: Inside the loop, three main actions occur:
The message
“Hello!”
is printed to the console.The program prompts the user with the question
“Do you want to end the loop? (Yes/No):”
and waits for the user’s input.If the user types
“Yes”
, theflag
variable is set tofalse
, and the message“good bye!”
is printed to the console.
Repetition: After each iteration, the loop checks the condition again. If
flag
is stilltrue
, the loop repeats the actions. This process continues until the user types“Yes”
(case sensitive).Termination: When the user types
“Yes”
, the conditionflag
becomesfalse
, so the loop stops running, and the program prints“good bye!”
.
Last updated