Page 75 - Touchcode_C6_Flipbook
P. 75

Pseudocode:
                 Program Start
                 set a to 0
                 while a is less than 4
                        increase the value of a by 1
                        display Hello
                 display world!!
                 Program End
                                                                                                      Subject Enrichment
                   Example 4: To print number from 5 to 1.

                 Condition: Till the time the test expression remains true, the body of the while loop printing
                 statement will run and once the condition is false the loop will stop executing.

                 Decision: Have we printed 1?
                 Pseudocode:

                 Program Start
                 set a to 5
                 while a is greater than or equal to 1
                        display the value of a
                        decrease the value of a by 1
                 Program End
                 For Loop
                 The for loop is used to repeat a sequence  a specific number of times. The for loop is
                 commonly used when the number of iterations is known in advance or when iterating over
                 a sequence, such as a list, string, tuple, or a range.

                 Using the range() Function

                 The range() function is an in-built function of Python. This function generates a list which
                 is a sequence type. A sequence is a succession of values bound together by a single name.
                 The range() function is used in the for loop to iterate over the numbers.
                 The general syntax of using the range() function is given below.
                 range(n): Generates a set of whole numbers starting from 0 to (n–1).

                 Example: range(6) is equivalent to [0, 1, 2, 3, 4, 5].
                 range(start, stop): Generates a set of whole numbers starting from ‘start’ to ‘stop–1’.

                 Example: range(3, 9) is equivalent to [3, 4, 5, 6, 7, 8].
                 range(start, stop, step_size):  By  default,  the  value  of  the  step_size  =  1  and  numbers
                 generated with a difference of 1. However, we can generate numbers by specifying the
                 value of step_size according to our requirement.
                 Example: range(1, 14, 2) is equivalent to [1, 3, 5, 7, 9,11,13].


                                                                                     Loops Using Block Coding    73
   70   71   72   73   74   75   76   77   78   79   80