Page 79 - Touchcode_C6_Flipbook
P. 79

Let’s understand  the  concept  of  exit  criteria with  an  example—creating a loop  to print
                 numbers from 1 to 100.

                 The exit criterion in this case is that the loop must stop when the 100th number is printed.

                 If the exit condition is not defined properly, the program may enter an infinite loop.

                 JUMP STATEMENTS

                 While executing a program, there are
                 times  when  we  need  to  change  the       While (Loop-Condition)
                 flow of execution. This means altering       {
                 the  normal  sequence  of  statements           Statement 1;
                 to jump from one part of the program            Statement 2;
                                                                 ...........
                 to another. For this we can use break           Break;             Execution  of the program
                 and continue statements.                        ...........        skips    after   the   break
                                                              }                     statement is encountered in
                 Break Statement                                                    the loop body.
                                                              Statement
                 The break statement alters or change
                 the normal flow of execution of the
                 statement by terminating the existing loop and continues the execution of the statement

                 following that loop.

                 Break statement is used to come out of a loop early when the specific condition is met.

                 In break, the control skips the lines after the break statement is found and executes the first
                 statement outside the loop.
                                                                                                       Critical Thinking
                   Example 1: Program to print number from 7 to 4, and another print

                   statement when you encounter break.

                 Pseudocode:
                 Program Start
                 set value of a to 7

                 while the value of a is greater that 0
                        display 'Current variable is: ' value of a
                        if value of a is equal to 4

                        break
                        decrease the value of a by 1
                 display Bye!
                 Program End




                                                                                     Loops Using Block Coding    77
   74   75   76   77   78   79   80   81   82   83   84