Page 286 - Ai_C10_Flipbook
P. 286

For example:
               [1]:   for Count in range(1,5,1):
                          print("Welcome")
                      print("Iteration Finished")
                      Welcome
                      Welcome
                      Welcome
                      Welcome
                      Iteration Finished

               [2]:   for A in range(1,11,2):
                          print(A,end=",")
                      1,3,5,7,9,

               [3]:   for Var in range(100,10,-10):
                          print(Var,end=",")
                      100,90,80,70,60,50,40,30,20,

               [4]:   for Var in range(5, 10, 2):
                          print(Var,end=",")
                      5,7,9,

              The While Loop
              The while loop is used to repeat a set of instructions as long as the condition is True. It means when the
              number of iterations is not fixed/indefinite before we start with the execution of a loop. It is therefore known as
              indefinite loop. Indentation of statements is must to specify the block of statements to be repeated using while
              loop. This loop is also called an entry-controlled loop as it checks for the condition in the beginning only. If the
              condition is True then the body of the loop will be executed. If the condition is False then it will not be allowed
              to enter within the loop and it stops.
              Syntax for the while loop:
                   while <condition>:
                     Statements
                     increment or decrement counter variable
              Where,
                   •  while  is a reserved keyword.
                   •   condition  is the criterion to repeat the instructions. The instructions repeat till the condition is True.
                     As soon as the condition is False it exits from the loop.
                   •   Statements are always indented can be single or a block. They are repeated till the condition is True.
                     Increment / decrement counter variable to change its value.
              Some examples of while loop are given below.

               [1]:   count = 1
                      while count <= 5:
                          print("Hello")
                          count+=1
                      print("Program ends")

                      Hello
                      Hello
                      Hello
                      Hello
                      Hello
                      Program ends


                    284     Artificial Intelligence Play (Ver 1.0)-X
   281   282   283   284   285   286   287   288   289   290   291