Page 94 - Computer - 8
P. 94

•  Test Expression: This is usually an expression, containing relational operators, which is evaluated
                to check if the loop is to be executed further or not. The loop execution continues until the value
                of the test expression is True.
             •  Body of Loop: This is the set of statements that will be executed repeatedly.
          Python provides two basic types of loops:
             •  For loop                             • While loop

          FOR LOOP
          The For loop is used when the number of times the loop is to be executed is known beforehand. That is
          why the For loop is called a Definite loop. The syntax of the For loop is as follows:
                 for <counter variable> in <sequence>:
                        body of for loop
                 other statements
          The flowchart to represent the For loop is given in the image.
          The <sequence> specifies the values that will be assigned to the counter
          variable in the For loop.  The counter variable starts with the first value
          in the sequence and executes the statements in the body of the loop.
          After the execution, it takes the second value and repeats the process.
          Once all the values in the sequence are finished, the control shifts to the
          next statement after the For loop block.


            Lab Activity 1                                                                        Experiential Learning
           To use For loop to display the numbers in a sequence.
           1. Type the code as shown in the image in the Python Editor.












           2. Press the F5 key to execute the code. Sample output of the program is given below.




















          The For loop works with strings in the same way. It considers a string as a sequence of characters. Thus,
          it starts with the first character in the string, executes the loop statements, takes the second character
          in the string, and repeats the process until it reaches the end of the string.



               92
   89   90   91   92   93   94   95   96   97   98   99