The Flowgorithm tool offers three loop structures for repetitive statements in the flowcharts. The Looping structures are:
- While Loop
- For Loop
- Do While Loop
The While loop statement is a repetition structure and is an indefinite loop. While loop structure is used when we don’t know how many iterations we need. When we want to loop until some condition is met etc.
While loop construct is a condition-controlled loop and is a pre-test loop structure. The loop condition is checked before executing the loop statements.
Add While Loop
To add a While loop construct, right click the mouse on the control line and choose the While symbol/shape.
Double click on the While symbol to provide the Boolean expression in the While properties window. For each iteration the Boolean expression is evaluated. If it expression evaluates to True, the loop statements are executed. This repetition continues until the Boolean expression evaluates to False.
Assume a flowchart to print user’s friends names. The flowchart will prompt the user to enter friend’s name one at a time. We will display the friend’s name on to the console window. The flowchart will exit when user enters “QUIT” as friend’s name.
Declare a variable to hold the user input.
Assign initial value to empty string.
Add a while loop to the flow chart. We use while because we don’t know how many names the user will enter during the flowchart runtime.
Add loop statements.
Execute the flowchart.
While loops are indefinite loops that can run forever. Some applications are designed to run for ever until they are interrupted by external signals. A real-world example of this an application/web server that runs forever to service the client requests. Let’s simulate a loop that runs forever until its stopped.
The while loop runs until the conditions becomes false. If we provide a condition that never evaluates to false the loop runs forever.
Examples, conditions like:
while(1 = = 1)
will always evaluate to True boolean truth value.
Sample run output
This flowchart will run forever until its is stopped by external signal like user stopping the flowchart or its encounters an runtime error.
Note that if the flowchart design doesn’t need an indefinite loop then this could be a defect or error in the flowchart design.
Flowgorithm Tutorials Link: