Lesson 14 of 55
8 mins readPython C-Style Three-Part for Loops
In Plain English
Perl supports traditional C-style `for (init; test; step)` loops for numeric iteration and matrix scanning.
Deep Dive: How It Works
Three Expressions: `for (my $i = 0; $i < $limit; $i++)`.
Lexical Iterator: Declaring `my $i` restricts the index variable to the loop block.
Step Variations: Step by any increment (e.g., `$i += 2`, `$i *= 10`).
Core Rules to Remember

C-Style Loops: Ideal when exact numeric indices or custom step sizes are required.

Lexical Loop Variables: Always declare `my $i` in the loop header to prevent variable leakage.
Live Interactive Example
Hit Run Code to see it liveCounting Even Numbers
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: $i started at 2, increased by 2 each step, and stopped after 8.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Print Multiples of 5
Use a `for` loop to print multiples of 5 from 5 up to 20 separated by spaces.
Print `"\n"` after the loop.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.