In pseudocode, a FOR loop is used when we want a set of instructions to repeat a specific number of times. Normally, the loop counter increases by 1 every time.
However, sometimes we need the counter to increase (or decrease) by a different amount.
This is where the STEP command is used.
What STEP Does
The STEP command changes the amount by which the loop counter is updated after each iteration.
-
Without STEP: counter increases by 1
-
With STEP n: counter increases by n
-
With STEP –n: counter decreases by n
This gives more control over loop execution.
Pseudocode Example Using STEP
Example 1: Counting in steps of 2
// Output even numbers from 2 to 10
FOR X ← 2 TO 10 STEP 2
OUTPUT X
NEXT X
Output: 2, 4, 6, 8, 10
How it works:
-
Loop starts at 2
-
Each time X increases by 2
-
Stops when X reaches 10
Example 2: Counting backwards
// Countdown from 10 to 0
FOR C ← 10 TO 0 STEP -1
OUTPUT C
NEXT C
Benefits of Using STEP
1. More flexible control over loop increments
You can choose any positive or negative step size instead of increasing by 1.
2. Makes code shorter and clearer
Without STEP, you would need to use a WHILE loop with manual counter updates.
3. Useful for specific sequences
-
Even or odd number generation
-
Skipping items in a list
-
Backward loops
-
Looping through every 3rd or 5th value
4. Reduces errors
The loop counter updates automatically according to the STEP value, so you avoid mistakes from manual incrementing.
The STEP command in a FOR loop allows the loop counter to change by a value other than 1. It helps create sequences that skip values or count backwards. This makes pseudocode easier to read and reduces the chance of errors.
