Have you ever ridden on a roller coaster with a loop? Sounds fun isn’t it? But I have not rode a roller coaster with a loop. So what’s with the loop? You may ask, well it is our lesson for this chapter. Chapter 2 : Looping. Yep, were on chapter 2.
Looping
So what is looping? It is basically repeating a process over and over again. It is like having ten sit-ups a day. So everyday you have to repeat the process, 10 sit-ups. We also have the infinite loop, where we process the data without end, but we don’t want to apply it to sit-ups.
We sometimes refer to the infinite loop as “hang”. We commonly hear our friends say “My Computer Hanged” or “My Computer Froze”, other call it “Bug”, sometimes it is caused by a Virus (which repeats a process over and over, then chokes the memory of the computer).
Okay, let’s say you want to print your name 3 times on the screen, without the loop, it can be done.
Flowchart 1:
And we have a flowchart that prints 3 “John”. How about printing it 100 times? Yes, your flowchart will be looooooooong, just to print the name “John”. This is where the loop comes in. We can repeat the name 100 times using loop, so our flowchart will be just a short one. I’ll give you two exactly the same example but different logic.
Logic 1
In Logic 1, we have minimized the flowchart by using a loop, if there is a certain portion of our code block that is to be repeated a number of times.
Algorithm:
1. start
2. Initialize the variable i = 1
3. we ask if the value of i is less than or equal to 100, else go to number 7.
4. we print “John”
5. we add 1 to the variable i = i + 1 (value of i is 1, hence = 1 + 1, the value of i now is 2, hence = 2 + 1, etc….)
6. repeat number 3.
7. end of the program
We can also draw the flowchart this way:
Logic 2
Logic 1, we ask first before we print the name
Logic 2, we print the name before we ask
Either way we still get the same result. So when making a flowchart, the path to take or the flow will all depend on your logic on how you solve a particular problem.
You can also control the looping, and it will be your decision as to when the program will end. Yes, you wish is the computer’s command.
Same as Logic 2, but with interaction with user.
Algorithm:
1. start
2. We initialized the value of Reply to space or blank
3. we print “John”
4. we ask if the user wants to print it again, user will enter either Y or N
5. get the user input
6. test if the input is Y or N, if Y, we repeat process number 3, else we go to number 7.
7. end of the program.
And there you have it. The flow terminates only when the user inputs ‘N’ and is assigned the variable Reply.
We can apply the logic to chapter 1, lesson 4, problem 5.
Original flowchart
Modified flowchart:
The original flowchart has 3 known Quiz results, compute and print for the average grade and stop, but the modified flowchart (as you noticed, I grouped similar items to one symbol), the 3 quizzes are unknown, so we let the user to input the values for the quizzes, then compute and print the average grade. The user now can repeat the process over and over, and exit or terminate the program when needed.
- End of Chapter 2, Lesson 1