The loop() Function and Repetition
Every Arduino sketch has a special loop() function that the board runs over and over, forever, after setup() finishes once. This is the outermost loop of your program and it is where continuous behavior lives — reading sensors, updating outputs, checking buttons. Inside loop() you also write your own for, while, and do-while loops to repeat a block a controlled number of times without duplicating code.
Cricket analogy: loop() is like an over that never ends: ball after ball, the bowler runs in, delivers, resets, and repeats. setup() is the one-time coin toss before the match begins.
The for Loop
A for loop is ideal when you know how many times you want to repeat. It has three parts in its header: initialization (int i = 0), a condition tested before each pass (i < 10), and an update run after each pass (i++). The body runs while the condition stays true. for loops shine for stepping through pins, fading an LED across brightness levels with analogWrite, or indexing into an array.
Cricket analogy: A bowler bowling a fixed six-ball over is a for loop: start at ball 1, continue while ball count is under 7, increment after each delivery. The over ends at exactly six.
void setup() {
// Fade an LED up then down using two for loops
pinMode(9, OUTPUT);
}
void loop() {
// Fade in: 0 -> 255
for (int b = 0; b <= 255; b++) {
analogWrite(9, b);
delay(5);
}
// Fade out: 255 -> 0
for (int b = 255; b >= 0; b--) {
analogWrite(9, b);
delay(5);
}
}while and do-while Loops
A while loop repeats as long as its condition is true and is best when you do not know the iteration count in advance — for example, while (digitalRead(button) == LOW) wait for a press. A do-while loop is similar but tests the condition after running the body, so the body always executes at least once. Both are common for polling a sensor until it reaches a target value.
Cricket analogy: A while loop is a batter facing balls until he is out: keep batting while not dismissed. You do not know the ball count ahead of time — the innings ends when the condition flips.
If a while loop's condition never becomes false, you create an infinite loop that freezes your sketch — loop() will never restart and other code never runs. Always make sure something inside the loop can change the condition, such as reading a fresh sensor value or incrementing a counter.
Use break to exit a loop early and continue to skip to the next iteration. Inside a for loop, break stops repetition entirely, while continue jumps straight to the update step (i++). These are useful for stopping a search as soon as you find a match.
delay() vs. Non-Blocking Timing
delay(ms) pauses the entire sketch, so nothing else runs during that time — buttons go unread and other sensors are ignored. For projects that must do several things at once, use millis(), which returns the milliseconds since the board started. By checking if (millis() - lastTime >= interval) you can time actions without freezing loop(), keeping the program responsive.
Cricket analogy: delay() is like the whole match pausing for a drinks break — nothing happens. Using millis() is like a fielder glancing at the clock between balls, staying alert while still tracking time.
- loop() runs forever after setup(); it is the outermost repetition of every sketch.
- Use a for loop when the number of iterations is known in advance.
- The for header has initialization, a pre-tested condition, and an update expression.
- while repeats while a condition holds; do-while always runs the body at least once.
- An unchanging condition creates an infinite loop that freezes the whole sketch.
- break exits a loop early; continue skips to the next iteration.
- Prefer millis() over delay() when the sketch must handle several tasks concurrently.
Practice what you learned
1. How many times does the loop() function run?
2. Which loop guarantees its body runs at least once?
3. In for (int i = 0; i < 5; i++), what is i++?
4. Why prefer millis() over delay() in a multitasking sketch?
5. What does continue do inside a for loop?
Was this page helpful?
You May Also Like
Conditionals in Arduino
Learn how if, else if, else, and switch statements let an Arduino sketch make decisions based on sensor readings and pin states.
Arrays in Arduino
Store and manage collections of related values using arrays, and combine them with loops to control multiple pins efficiently.
Functions in Arduino Sketches
Learn how to define, call, and return values from functions to organize Arduino code and avoid repetition.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics