In my previous post, I demonstrated a general overview of the flowcharting process. Today I plan on delving more into two types of flows. The Decision and the loop.
The basis of a decision statement is this: If a condition is true, then perform one action, if it’s false, perform another action. In fact, an If-then is one type of decision that you will encounter in pseudocode and code. The decision is also an integral part of most loops. A loop carries out a set of actions, and checks to see if a condition is true before continuing or exiting.
In a flowchart, you do not include the “if” or the loop name. You simply put the question portion in (the condition being checked, and the value it’s being checked against). This is because you are only concerned about the flow of the program, not the syntax. For example, if you wanted to check to see if a wagon was red, you would create the symbol like this:
While- endwhile loop:
A while- endwhile loop used to be called a “While do” loop. Basically the premise is while a condition is true, perform some actions. The most common type of while loop occurs when you are reading data from a file. You want to continue processing the data until you hit the end of the file. Then you want to get another line of data, and check that to see if it’s the end of file.
While- endwhile loops can be used for any number of situations. When I’ve explained a while- endwhile lopp to people, I told them that you get a chunk of data, ask if it’s the end of file, if not then perform actions on that data, then get another chunk and repeat… A while- endwhile loop is an “ask first, shoot if necessary” type of situation.
Here is an exaple of a while- endwhile loop, where you get data, check to see if it’s an end of file (eof) and process it if not. You repeat the loop until you hit the EOF.
Do- Until loop:
A Do- Until loop is basically a “shoot first, ask questions later” type of loop. You will perform a set of actions, then check to see if you need to continue to perform them. If so, you will loop back to the beginning of the actions. If not, then you will exit the loop and continue on with the program.
The Do- until loop must be executed at least once, and is executed until a condition becomes true. One way of looking at it is that it is a “while condition is false, do” loop. These loops are used less frequently than other loops, so you don’t need to be too concerned with them. However, you should understand the structure. In COBOL, these are considered “Perform Until” and in Pascal, they are “Repeat Until” loops.
An example of a Do- Until loop is below. In this example, you will print the value of the variable until it reaches 5. Then you will print “All done.”
A For loop is used when you know exactly how many times you want the loop to be executed. That is the main difference between this loop and the previous two loops. In the previous loops, they execute until a condition becomes true. In a for loop, while you still check a condition, you know exactly how many times it should execute.
For loops are useful in a lot of places. The best example of using a for loop is when you deal with arrays. I will describe arrays more when we get into pseudocode.
In the following example, you will see how a for loopo works. You will notice that it’s similar to the Do- Until loop above, but should not be confused with that.
You will probably notice that I didn’t use While, If, Do Until, or For in any of these examples. Again, these are a graphical indication of the flow. The syntax comes later. In my next post, I will take a look at a special type of decision called a select- case, or just a case.
Have a great day:)