For Loops

beginner45 min

For loops

Use a for loop when you know how many iterations you need. Initialization, condition, and update sit in the header.

For loop structure

Examples

Calculations inside a for loop

When a for loop fits

  • Fixed iteration count (array length, number of samples)
  • Index-based array access
  • Accumulators updated each pass (sum, average)

Try It Yourself:

Exercise 1: For Loop Practice

  • Create a multiplication table using nested for loops
  • Build a robot sensor calibration system with progress tracking
  • Calculate factorial using for loops
  • Create a motor test sequence for all robot motors
Practice using for loops for different scenarios: