🎓 Prepared by students from Boğaziçi University

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.

Short answer

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.

How a Loop Executes
1234
  1. 1.InitializeSet the counter to its starting value
  2. 2.Check conditionTest whether the loop should continue
  3. 3.Run bodyExecute the code inside the loop
  4. 4.UpdateChange the counter (e.g., add the step)
01

Try it: interactive calculator

Number of iterations
11
= (10-0)/1+1
02

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)
03

Flashcards

04

Quick quiz

Q1.A for loop runs from i=0 to i=9, step 1. How many times does it execute?

Correct answer: B. n = (9−0)/1+1 = 10 iterations.

Q2.Which loop type is best when you don't know in advance how many times to repeat?

Correct answer: B. A while loop repeats based on a condition, ideal when the count isn't known ahead of time.

Q3.What causes an infinite loop?

Correct answer: B. If the condition never becomes false, the loop never stops.

Q4.In the formula n = (end−start)/step + 1, what does step represent?

Correct answer: B. Step is how much the counter increases (or decreases) on each pass.
📄Download this topic as a printable worksheet (PDF)Summary + 10 questions + answer key — print it, share it in class.
Study better with Bounlu apps
Notek
Notek

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.

Get it free
Notek 1Notek 2Notek 3Notek 4Notek 5
05

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.

06

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.

Related topics