Looping
Flowchart 1:
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