Chapter 6-1 : the counter trick

 the counter trick 

In programs, you very often have to perform repetitive tasks such as reading mathematical data, customer names, product characteristics, etc. from the keyboard, and at the end of the day, you need to know how much data has been read.

The idea is not to read "a lot" of customer names, but to read one customer name at a time, in a repeating loop. All that's needed is a small memory box, a counter, to which 1 is added at each loop. When the loop is complete, we simply read this little memory box to see what value it contains, which represents the number of turns of the loop and therefore the number of customer names that have been read. Important: before entering the loop, it's essential to initialize this little memory box to 0.

For your information, this little memory box will be called a "variable", as its contents can change constantly. Each variable is identified by a unique name which, in the example below, is in underlined italics.

Here's how this kind of program works:

1. Set 0 in the counter variable
3.      Carry out the requested process
4.      Adds 1 to the counter variable
5. UNTIL the requested process ends
6. Display "Process completed", counter, "times."

Put this into practice by performing the museum guard robot exercise.

Have a good work.