When I first started learning programming, one of the most useful tools I discovered was the FOR loop. It’s a simple yet powerful way to repeat actions, and understanding it through pseudocode made everything so much easier. In this article, I’ll walk you through the basics, some common examples, and even a few advanced uses that helped me master the concept.
🔹 What Is a FOR Loop in Pseudocode?
A FOR loop is designed to run a set of instructions a specific number of times. In pseudocode, we write it in a way that’s easy to read and understand, without worrying about exact programming syntax.
Here’s what a basic FOR loop looks like in pseudocode:
1 2 3 4 5 | FOR counter ← start_value TO end_value [STEP increment] // Actions to perform END FOR |
The idea is simple: start from a number, repeat the actions, and move up (or down) until you reach another number. The optional STEP
lets you change how much the counter increases each time.
Let’s take a basic example: printing the numbers 1 to 5.
1 2 3 4 5 | FOR i ← 1 TO 5 OUTPUT i END FOR |
When I first wrote this down, it amazed me how easily I could represent a repetitive task!
🔹 Practical Examples of FOR Loops
Here are a few examples that really helped me understand how FOR loops can be used in real-world situations.
1. Summing Numbers from 1 to 100
One of the first exercises I tried was adding numbers from 1 to 100. Here’s how the pseudocode looked:
1 2 3 4 5 6 7 | sum ← 0 FOR i ← 1 TO 100 sum ← sum + i END FOR OUTPUT sum |
Every time the loop runs, it adds the current number (i
) to the sum
. After the loop ends, sum
holds the total.
2. Asking for User Input and Calculating Average
Another project I remember was collecting numbers from the user and finding the average.
1 2 3 4 5 6 7 8 9 10 11 | sum ← 0 FOR i ← 1 TO 50 OUTPUT "Enter a number:" INPUT num sum ← sum + num END FOR average ← sum / 50 OUTPUT "Total:", sum OUTPUT "Average:", average |
This example taught me how loops could interact with users and perform calculations based on their input — a huge step toward building real applications.
3. Summing Elements of an Array
Working with arrays is a big part of programming, and FOR loops make it easy to process each element.
1 2 3 4 5 6 7 8 | numbers ← [65, 45, 10, 7, 125] sum ← 0 FOR i ← 0 TO LENGTH(numbers) - 1 sum ← sum + numbers[i] END FOR OUTPUT "The total sum is:", sum |
I realized that by using the array’s length, I could loop through any list of numbers, no matter how long it was.
🔹 Going Further: Step Values and Nested FOR Loops
Once I was comfortable with basic loops, I started exploring more advanced techniques.
Using STEP to Control the Counter
Sometimes you don’t want to increase by 1 — maybe you want every second number instead.
1 2 3 4 5 | FOR i ← 0 TO 10 STEP 2 OUTPUT i END FOR |
This loop prints even numbers between 0 and 10. Changing the step value opened up lots of creative possibilities for my code.
Creating a 3×3 Matrix with Nested FOR Loops
Nested loops (a FOR loop inside another FOR loop) were a little tricky at first, but they’re super useful for working with grids, tables, or matrices.
1 2 3 4 5 6 7 | FOR i ← 1 TO 3 FOR j ← 1 TO 3 OUTPUT "(" + i + "," + j + ")" END FOR END FOR |
This pseudocode prints out all the coordinates in a 3×3 grid, like (1,1), (1,2), (1,3), and so on. It’s the foundation for understanding things like two-dimensional arrays.
✨ Final Thoughts
Learning how to use FOR loops through pseudocode made my early programming days much less intimidating. Pseudocode strips away the messy parts of coding and lets you focus on the logic — and once you get that logic right, moving to real code becomes much easier.
Whether you’re building a simple calculator or designing complex systems, mastering FOR loops will always be one of the best tools you can have in your programming toolkit.
👉 If you’d like to see more pseudocode examples, click here to explore them!