Page 281 - Computer_Science_F5
P. 281

Computer Science  Table 4.13 shows the difference between while...loop and do-while...loop
           statements.

           Table 4.13: Difference between while...loop statements and do-while…..loop statements

            While loop
                                                    Do-while loop
          FOR ONLINE READING ONLY
            Test the condition  before  execution;  Body execution takes place once before
            if the  condition  is true,  the  execution  testing the condition.  Then, if the
            continues until the condition becomes  condition is true, the execution continues
            false.                                  otherwise, the execution stops.
            Testing of the loop body occurs at the  Testing of the loop body occurs at the
            top.                                    bottom.
            The loop stops execution if the condition  Loop is executed  at least  once even
            is false.                               though the condition is false



           For loop                                  Table 4.14: Body parts of ‘for….loop’
           For loop is a predetermined type of loop.   Part        Function
           It allows a user to enter  the required   Initialisation  It initialises a variable
           number of iterations to be done before                  counter
           looping. For example, the program will    Condition     It tests the loop condition
           prompt a teacher to enter the number of   Increment/    It provides an increment
           students before calculating their average   decrement   or  decrement  variable
           marks to prepare the number of iterations.              counter.
           For loop contain variables that control
           how many times the loop execution can     Figure 4.50 shows the execution flowchart
           occur. Unlike other control structures,   of the for loop.
           for...loop  contains three body features:
           initialisation, condition, and increment/
           decrement counter. The syntax of for…           Initialization
           loop is given as :                                               Increment/
                                                                            Decrement
               for (initialisation; condition ;

               increment/decrement counter)                                 Loop body
               {                                             Condition          True
               loop body
               }                                                  False
           Table 4.14 shows the function of each
           body part of the for…loop                    Figure 4.50: A flowchart of “for loop”


                                                 272
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   272                                                     23/07/2024   12:34
   276   277   278   279   280   281   282   283   284   285   286