What is a Loop in Programming?
A loop is a programming structure that repeats a block of code multiple times, so you don't have to write the same instructions over and over. The two most common kinds are the for loop, which repeats a known number of times, and the while loop, which repeats as long as a condition stays true.
A loop repeats a set of instructions until a condition is met or a fixed number of iterations completes, letting a program automate repetitive tasks efficiently.
- 1.Initialize — Set the counter to its starting value
- 2.Check condition — Test whether the loop should continue
- 3.Run body — Execute the code inside the loop
- 4.Update — Change the counter (e.g., add the step)
Try it: interactive calculator
Step-by-step worked examples
A for loop runs from i = 0 to i = 4, increasing by 1 each time. How many times does it execute?
start = 0, end = 4, step = 1 n = (end − start)/step + 1 n = (4 − 0)/1 + 1 = 5 The loop body runs 5 times (i = 0,1,2,3,4)
A while loop keeps running while count < 20, starting at count = 0 and adding 5 each time. How many iterations?
This matches a loop from start=0 to end just below 20 with step 5 Valid values: 0,5,10,15 (20 fails the condition) n = (15 − 0)/5 + 1 = 4 The loop runs 4 times
A for loop goes from i = 1 to i = 10, step 2. How many iterations?
start = 1, end = 9 (last valid value ≤10 with step 2 from 1 is 9) n = (9 − 1)/2 + 1 n = 8/2 + 1 = 5 The loop runs 5 times (i = 1,3,5,7,9)
Flashcards
Quick quiz
Q1.A for loop runs from i=0 to i=9, step 1. How many times does it execute?
Q2.Which loop type is best when you don't know in advance how many times to repeat?
Q3.What causes an infinite loop?
Q4.In the formula n = (end−start)/step + 1, what does step represent?
The full card deck, worked steps and AI-tutor support for “What is a Loop in Programming?” are in Notek — study by hand before your exam.
Common mistakes
Forgetting to update the loop counter, causing an infinite loop. — Correct: Always make sure the condition variable changes each iteration so the loop can end.
Using a for loop when the number of repetitions isn't known in advance. — Correct: Use a while loop when the stopping point depends on a condition rather than a fixed count.
Off-by-one errors, like looping one time too many or too few. — Correct: Double-check boundary values (e.g., <= vs <) against the formula n = (end−start)/step + 1.
Assuming step is always 1. — Correct: Step can be any number — 2, 5, or even negative for counting down.
FAQ
What is a loop in programming?
A loop is a control structure that repeats a block of code either a fixed number of times or while a condition remains true.
What is the loop iteration formula?
n = (end − start)/step + 1 gives the number of iterations for a counted loop.
What are examples of loops?
A for loop printing numbers 1 to 10, or a while loop that keeps asking for input until the user types 'quit'.
How do you calculate the number of loop iterations?
Subtract the start value from the end value, divide by the step size, then add 1.




