Page 67 - Code & Click - 8
P. 67
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.
Experiential Learning
Lab Activity 1
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.
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.
Experiential Learning
Lab Activity 2
To use For loop to display each character in a string.
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.
USE OF RANGE( ) FUNCTION IN FOR LOOP
The Range( ) function is used to specify a range of values which are assigned in sequence to the
counter variable in the For loop. The syntax of the Range( ) function is:
range( [Start_val], End_val, [Step_val] )
65