Page 74 - Touchcode_C6_Flipbook
P. 74

Different types of loops are:
                  1.  While Loop
                  2.  For Loop

                  While Loop/Conditional loop

                  While Loop executes a statement or group of statements till a given condition is true. Once
                  the condition is false, the loop is terminated.

                                                                                                       Subject Enrichment
                    Example 1: To print number from 1 to 10.


                  Condition: Till the time a is less than 10, the body of the while loop will run.
                  Decision: Have we reached number 10, if yes stop the loop.

                  Pseudocode:
                  Program Start
                  set a to 0

                  While a is less than 10
                         increase the value of a by 1
                         display a
                  Program End
                                                                                                       Subject Enrichment
                    Example 2: To print a statement five times by using a while loop.


                  Condition: Till the time the test expression remains true, the body of the while loop will run.

                  Decision: Have we printed the statement five times or not?

                  Pseudocode:
                  Program Start
                  set a to 0

                  while a is less than 5
                         increase the value of a by 1
                         display Hello World
                  Program End
                                                                                                       Subject Enrichment

                    Example 3: To print a statement four times after that print a different
                    statement.


                  Condition: Till the time the test expression remains true, the body of while loop prints
                  statement 1 and after the condition is false, it will print statement 2.
                  Decision: Have we printed statement 1 four times, if yes then print statement 2.



                   72     Touchcode-VI
   69   70   71   72   73   74   75   76   77   78   79