Page 72 - ModularV1.1 _c6_flipbook
P. 72

LOOPING

                  To print the numbers from 10 to 1 you can create a program in two ways. You can either give
                  separate instructions to print each output or give a single set of instructions to print the output.
                  This set of instructions will repeat for a fixed number of times or until the condition is satisfied.
                  This is  called  looping.  In  BASIC-256, the  statements used  for  repetition  of  instructions  in a

                  program are called iterative statements or looping statements. Looping statements are very
                  useful and necessary for developing an application. BASIC-256 provides FOR...NEXT and WHILE...
                  END WHILE as looping statements. Let's learn about them.
                      FOR…NEXT STATEMENT

                  The FOR...NEXT statement is used to run a code a fixed number of times. To manage this kind
                  of loop, we create a variable that tracks how many times the loop will run. Generally, this variable
                  is called a count control variable. The basic syntax of the FOR...NEXT statement is as follows:
                  FOR counter = startNumber TO endNumber
                         Statements to repeat
                         Increase or decrease the counter variable
                  END
                  Where
                       the counter is a variable that keeps count of the number of times the instructions inside the
                     loop have been executed.
                      the startNumber is the starting number of value of the loop.
                      the endNumber is the total number of times the loop must run.
                  Let us see a program to understand the concept
                  of FOR...NEXT statement. This  program  prints
                  the numbers from 1 to 15 in order. The concept
                  of increasing the value of a variable  is used
                  in programming  very  often.  So, most of  the
                  programming  languages have a simpler and

                  easier way to do this. In the preceding example,
                  the variable i contains a value that increases by 1
                  each time the loop runs.                                           Printing numbers 1 to 15

                      SAMPLE PROGRAMS USING FOR...NEXT STATEMENT

                  Program 4: To print even numbers up to 30.


















                  70      Modular (Ver. 1.1)-VII
   67   68   69   70   71   72   73   74   75   76   77