Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 9 : Looping : For … Next

The computer is very good at following instructions.  Its ability to do what it is told to do, over and over again, is what makes the computer a valuable tool.  In this chapter we will learn how to instruct a computer to do the same thing several times. 

This ability is called looping.  It is called looping because in the early days of computers programmers drew pictures of their programs before they ever wrote code.  These pictures were called “flow charts.”  In a flow chart a statement in a program was shown as a box.  The next statement was connected to the first by a line with an arrow on it.  If a programmer wanted to repeat a step in the process, they would show it by drawing a line back up to the previous step – forming a loop.  (There is also the story of how paper tape with little holes in it was used to instruct the computer.  To make the computer repeat instructions over and over, the programmer would connect the end of the tape to the beginning – forming a loop.)

In B2C the loop is implemented by the For command.  The For command has three parts: the initialization, the final value, and the (optional) step.

for [initialization] to [final value] [step]
for i=0 to 3 step 1
    'statements go here
next

The ‘initialization’ part uses an un-Dim’d variable name (like i) and is initialized to the start value of the loop.  The ‘to’ part declares the last value of the loop and the ‘step’ declares the amount to increment ‘i’ inside the loop.  If the ‘step’ part is omitted “step 1” is assumed.  The word “next” indicates the end of the loop and tells B2C to increment the variable by the step value and to go back to the top of the loop (the statement just after the for).  If the variable has reached the ‘final value’ then processing continues on the statement after the next.

 For readability, the statements inside the for loop are indented 3 or 4 spaces.

 It is possible to exit a For loop early by executing the "Exit For" command in the middle of the loop.

Historical Note: The variables i, j, k, l, m, & n are favorite variables for loops.  This is for two reasons.  First, the single letter makes for easier typing as a subscript to an array variable.  Second, in the early days of programming (1960s) there was a language called FORTRAN that had no DIM statement. Instead, all variables were named a-z, and were assumed to be Double (floating point).  Except for the variables i-n, which were integers.  Why i-n?  Because i & n were the first 2 letters of the word “integer”.

Infinite Loops

It is possible to get caught in a loop that never exits.  This is known as an infinite loop.  A for loop with a zero step size would create an infinite loop

for i=0 to 0 step 0
   print i
next

The example code above will print a screen ful of zeroes.  To get out of an infinte loop, press the ESC key.


Example program:

DO THIS

Copy the file "c:\…\B2Cv5\tutorial\ch9.b2c" to "C:\…\B2Cv5\ch9.b2c".  Then execute the command "build ch9.b2c".  Download the ch9.app file to the cybiko.
With the introduction of loops, our program becomes even simpler.  Now we don't have to declare the number of grades up front. It becomes a variable we get from the user at the start.
      ' chapter 9 example program
      ' sum and average of n grades
      ' grades are from 0-100
      dim sum as int 'this variable will store the sum of the n grades
      dim avg as int 'this variable will store the average of the n grades
      dim n as int   ' the number of grades to average
       
      print "How many grades?"
      input n
      dim grade[n] as int ' an array of n grades
       
       ' ---INPUT ---
      for i=0 to n-1          'get the inputs
          print "Enter grade ", i   ' notice that we indent in loops
          input grade[i]      'get the grades from the student
      next
       
      '--- PROCESS ---
      ' compute the sum and the average
      for i=0 to n-1
          sum = sum + grade[i]   'notice we accumulate the sum
      next
      avg =       sum/n          'compute the average
       
      '--- OUTPUT ---
      print "The average of your"
      print n, " grades is", avg
       
      print "Press <Enter> to continue"
      dim tmp ' a temporary variable
      input tmp 'wait for the user to press enter