Use pseudocode to write: a ''count-controlled'' loop

Resources | Subject Notes | Computer Science

11.2 Constructs - Count-Controlled Loop

11.2 Constructs - Count-Controlled Loop

Objective

Use pseudocode to write a 'count-controlled' loop.

What is 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.

Pseudo-code for a Count-Controlled Loop

The general structure of a count-controlled loop in pseudo-code is as follows:

  1. Initialization: Set a counter variable to a starting value (usually 0).
  2. Loop Condition: Check if the counter variable is less than or equal to a specified end value.
  3. Loop Body: If the loop condition is true, execute the statements within the loop body.
  4. Update Counter: Increment or decrement the counter variable by a predefined value.
  5. Repeat: Go back to step 2 and repeat until the loop condition becomes false.

Example: Printing Numbers from 1 to 5

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.

Complete Pseudo-code

Here is the complete pseudo-code for the example:

    count = 1
    Loop: While count <= 5
        Print count
        count = count + 1
    

Diagram

A diagram illustrating the execution of this loop could be represented as follows:

Suggested diagram: A flowchart showing the loop with the counter variable and the print statement.