Resources | Subject Notes | Computer Science
Use pseudocode to write a 'count-controlled' loop.
A count-controlled loop is a type of loop that executes a specific number of times. The number of iterations is determined by a counter variable, which is initialized before the loop begins and typically incremented or decremented within the loop.
The general structure of a count-controlled loop in pseudo-code is as follows:
Let's consider an example where we want to print numbers from 1 to 5 using a count-controlled loop.
Here's the pseudo-code:
Step | Action | Explanation |
---|---|---|
1 | Set count to 1 | Initializes the counter variable. |
2 | Loop: While count <= 5 | Starts the loop, which will continue as long as the counter is less than or equal to 5. |
3 | Print the value of count | Executes the statement within the loop body, printing the current value of the counter. |
4 | Increment count by 1 | Increments the counter variable, moving towards the loop termination condition. |
Repeat steps 2-4 | The loop continues until count becomes greater than 5. |
Here is the complete pseudo-code for the example:
count = 1 Loop: While count <= 5 Print count count = count + 1
A diagram illustrating the execution of this loop could be represented as follows: