Page 149 - Computer - 6
P. 149

Example 3: Write a program to accept name and age of a user and display whether the user is eligible
            to vote or not.

            The code and sample output for the example are given below.














            Looping Statements

            A Looping Statement or Loop is used to execute an instruction or a group of instructions multiple
            times or until a specific condition is satisfied. Each execution of the set of instructions through a loop
            is called an Iteration. Small Basic uses two types of looping statements:

            (a)  For...EndFor Loop
            The For...EndFor loop, or For loop, statement is used to execute a set of instructions a specific number
            of times. It is useful when the number of iterations is known beforehand. The For...EndFor loop lets
            you define a variable for the number of iterations, called the counter variable, and assign it an initial

            and final value.
            The For loop executes till the value of the counter variable is less than or equal to its final value. After
            each iteration, Small Basic increments the value of the counter variable by the stepvalue as specified
            after the Step keyword. The default value for Step is 1. The loop terminates when the value of the
            counter variable exceeds its final value. The syntax for the For...EndFor loop is:

                 For VariableName = InitialValue to FinalValue Step stepvalue
                        <instructions to be executed>
                 EndFor

            (b)  While…EndWhile Loop

            The While...EndWhile loop, or While loop, is used to execute a set of instructions repeatedly based
            on a condition. The While loop executes till the specified condition evaluates to True. It is useful
            when the number of repetitions is not known beforehand. The While...EndWhile loop uses a counter
            variable which was already defined earlier. The value of the counter variable is incremented inside
            the While loop. There is no final value fo the counter variable in the While loop. The loop terminates
            when the specified condition evaluates to False. The syntax for the While...EndWhile loop is:

                 While (Loop Condition)

                        <instructions to be executed>
                        <Counter variable increment statement>
                 EndWhile



                                                                                                             147
   144   145   146   147   148   149   150   151   152