Page 74 - TP_V5.1_C8_fb
P. 74

Pragram 1: To print odd numbers till 10 using start, stop and step values of a range function.

                      Program1.py                                               Output
                   File  Edit  Format   Run   Options  Window    Help        1
                                                                             3
                   for i in range (1,10,2):
                                                                             5
                       print(i)
                                                                             7
                                                                             9



                  Program 2: To print a selected range of numbers with start and stop values.

                      Program2.py                                               Output
                   File  Edit  Format   Run   Options  Window    Help        3
                                                                             4
                   for i in range (3,7):
                                                                             5
                       print(i)
                                                                             6



                  Program 3: To print whole numbers from 0 to 9 using only stop value.

                      Program3.py                                               Output
                   File  Edit  Format   Run   Options  Window    Help        0
                                                                             1
                   for i in range (5):                                       2
                       print(i)                                              3
                                                                             4



                  THE while LOOP

                  The while loop repeats a block of statements while a given condition is true. The while loop is
                  commonly  used  when  the number of iterations is not known.   In  this looping  construct,  first
                  the condition is checked, and if it is true, the control will move inside the loop and execute the
                  statements until the condition becomes false. If the initial condition is false, then the control will
                  not enter the loop block. The syntax of while loop is as follows:

                  while(Condition):
                      statement(s)
                      increment/decrement
                  Program 4: To demonstrate the use of a while loop.
                      Program4.py
                   File  Edit  Format   Run   Options   Window   Help           Output
                                                                              Count is: 1
                   count = 1
                                                                              Count is: 2
                   while count < 5:
                                                                              Count is: 3
                       print("Count is:", count)
                       count += 1                                             Count is: 4






                   72   Premium Edition-VIII
   69   70   71   72   73   74   75   76   77   78   79