What is a For Loop?
A For loop is used when you know exactly how many times you want to repeat something. Unlike a While loop that depends on a condition, a For loop counts through a range of numbers automatically.
In PictoBlox, the Repeat (n) block acts as a For loop. It repeats its contents exactly n times, automatically keeping count for you.

For Loop vs While Loop
For Loop vs While Loop
Example: Drawing Multiple Squares
Let's use a For loop to draw 5 squares on the Stage, each rotated slightly from the last to create a pattern.
- Open PictoBlox and add the Pen Extension.
- Add a 'When flag clicked' block, then 'Erase All' and 'Go to x:0 y:0'.
- Add a 'Repeat 5' loop.
- Inside the outer loop, add a 'Repeat 4' loop with 'Move 100 steps' and 'Turn 90 degrees' to draw a square.
- After the inner loop, add a 'Turn 30 degrees' block to rotate before drawing the next square.
- Add 'Pen Down' before and 'Pen Up' after.


Loops can be nested inside each other! An outer loop can contain an inner loop, and the inner loop will complete all its repetitions for each single repetition of the outer loop.

.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)