Dhakshina Vijayaprakash - 04 Iteration (Count Controlled) - Procedural Python
Dhakshina Vijayaprakash - 04 Iteration (Count Controlled) - Procedural Python
Iterations are a programming construct which enables the repeated execution of lines of code.
The FOR loop is a type of iteration which is count controlled. This means that it loops for a set
number of iterations.
The variable x, counts from zero to the number in the brackets, so to keep count of the number of
iterations the loop has to perform. Therefore, on the first iteration, x is zero, as shown by the first
print statement.
The variable x then counts on to 1, and this is shown in the second print statement.
The variable x continues to count on, until it reaches the number in the brackets, at which point
the loop ends.
Notice how 5 is not outputted. The moment x becomes 5, is the moment the loop ends, so there is
no print statement execute to display the number 5.
A More Advanced FOR Loop
In python, there are actually some other values that we can use to have more control over our
FOR loop as shown below:
In this example, you can see that the bracket contains (0,10, 2).
This means that the loop has been set up to count from 0 to 10, but counting up in 2s.
Therefore, the variable x counts 0, 2, 4, 6, 8 (stopping the loop at 10) therefore executing 5 times,
printing out the 5 print statements that you can see above.
Hopefully the above has given you an understanding of how FOR loops can be set up and coded
in Python. However, the most effective way of truly understand how to program FOR loops, is to
get stuck into some practical programming tasks.
Below are a series of tasks designed to guide you through programming FOR loops in the python
programming language.
PRIMM TASKS
1: Predict
Code
I think that the code will subtract from 10. It will then print the numbers subtracted by
one.
>>> 10
>>> 9
Predictions
>>> 8
>>> 7
>>> 6
>>> 5
>>> 4
>>> 3
>>> 2
>>> 1
>>> 0
>>> Blast Off!
2: Run
Yes, there were a few differences- it didn’t create a new prompt for each number.
3: Investigate
What happens if you alter It does the same thing, it just counts backwards from 20 instead of 10.
the value in the range()
brackets to 20? Explain
the result.
What happens if you put I changed the range() brackets to (5, 20). It then counted down from
another number BEFORE 15 to 1.
the current number in the
range() brackets (e.g.
range(5, 10)? Explain the
result.
4: Modify
5: Make
Create a program in python for each point below and add a screenshot of your code in the boxes
provided:
1) Write a program that displays the word ‘Hello’ on the screen 4 times using the ‘for loop’.
for _ in range(4):
print(“Hello”)
2) Write a program which prompts the user to enter a short message and the number of times it is
to be displayed and then displays the message the required number of times.
3) Write a program to display the squares of all the integers from 1 to 12 in two columns headed
‘Number’ and ‘Square of Number’.
4) Write a program that asks the user to enter the number of stars per row and the number of
rows to be displayed. For example, entering 5 and 3 should display:
*****
*****
*****
5) Write a program that asks the user to enter how many numbers are to be averaged, then asks
the user to enter these numbers in turn, then calculates the average. The program should
display the average on the screen.
6) Write a program to display an ‘n times table’ for a given integer ‘n’. For n = 4, the output
should be:
1*4=4
2*4=8
3 * 4 = 12
12 * 4 = 48