aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer ScienceTopic 6.3Loops
Back to Computer Science Topics
6.3.36 min read

Loops

IB Computer Science · Unit 6

AI-powered feedback

Stop guessing — know where you lost marks

Get instant, examiner-style feedback on every answer. See exactly how to improve and what the markscheme expects.

Try It Free

Contents

  1. 1Doing something more than once
  2. 2Counted or conditional
  3. 3Conditions inside loops
  4. 4Exam-style question
The big idea: A loop repeats a block of code.

The only real question is what decides when to stop: a count you already know, or a condition you keep testing.
# Counted loop — you know how many times
for i in range(5):
    print(i)              # 0 1 2 3 4

for mark in marks:        # once per item, count taken from the data
    total = total + mark

# Conditional loop — you do not know how many times
guess = ""
while guess != answer:
    guess = input("Guess: ")
print("Correct")
// Counted loop — you know how many times
for (int i = 0; i < 5; i++) {
    System.out.println(i);        // 0 1 2 3 4
}

for (int mark : marks) {          // once per item, count taken from the data
    total = total + mark;
}

// Conditional loop — you do not know how many times
String guess = "";
while (!guess.equals(answer)) {
    guess = scanner.nextLine();
}
System.out.println("Correct");
A counted loop of 5 runs 0 to 4: Five values, starting at 0 and stopping before 5.

The same rule as string slices, and the same source of off-by-one errors.

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime

Counted — for

  • The number of repeats is known before it starts
  • The counter is managed for you, so it cannot be forgotten
  • Cannot loop forever by accident
  • Right for: every item in a list, a fixed number of attempts, rows in a grid

Conditional — while

  • Repeats as long as a condition holds
  • May run any number of times, including none at all
  • You must change what the condition depends on
  • Right for: waiting for valid input, reading until end of file, repeating until solved
The choosing question: Do I know how many times before I start?

Yes → counted loop. No → conditional loop. Answering it first prevents most loop problems.
A while loop may run zero times: The condition is tested before the first pass.

If it is already false, the body never runs at all — which is correct behaviour, and is often exactly what you want.

Trace a loop with a table: one row per pass, and check the condition every time before the body runs.

A while loop traced pass by pass until its condition fails
A while loop checks its condition before every pass

Memorize terms 3x faster

Smart flashcards show you cards right before you forget them. Perfect for definitions and key concepts.

Try Flashcards FreeYour first topic is free to keep • No credit card required
# Counting only what matches
passes = 0
for mark in marks:
    if mark >= 50:
        passes = passes + 1

# Stopping early once the answer is found
found = False
for name in names:
    if name == target:
        found = True
        break                 # no reason to keep looking

# Validating input: repeat until it is acceptable
age = -1
while age < 0 or age > 120:
    age = int(input("Age: "))
// Counting only what matches
int passes = 0;
for (int mark : marks) {
    if (mark >= 50) {
        passes = passes + 1;
    }
}

// Stopping early once the answer is found
boolean found = false;
for (String name : names) {
    if (name.equals(target)) {
        found = true;
        break;                    // no reason to keep looking
    }
}

// Validating input: repeat until it is acceptable
int age = -1;
while (age < 0 || age > 120) {
    age = Integer.parseInt(scanner.nextLine());
}
1

if inside a loop

Runs the test once per item, so it counts, filters or finds.

The counter for what matched belongs outside the loop — inside, it would reset every time round.

2

break — leave early

Stops the loop immediately. Right when there is genuinely nothing left to learn, as in a search that has found its target.

It turns a search that always costs n into one that usually costs less.

3

Nested loops

A loop inside a loop runs the inner one completely for each pass of the outer.

That is why a grid costs rows × columns — and why nested loops are where O(n²) comes from.

4

Validation loops

while the input is unacceptable, ask again.

Start the variable at a value that fails the test, so the loop is guaranteed to run at least once.

The infinite-loop check: In any while loop, point at the line that changes what the condition tests.

If you cannot find it, or it sits inside an if that might not run, the loop can go on forever.

How this is tested — you must choose the right loop and get the number of repeats exactly right. It comes up two ways:

Paper 2 — working with code

  • Construct a loop for a described task, 3-5 marks
  • Trace a loop and state the output
  • Explain why a loop runs the wrong number of times

Paper 2 — the algorithmic-thinking question

  • Choose counted or conditional from a description
  • Explain how a loop terminates, with no code shown
The classic trap: Off by one on the repeat count. A counted loop of 5 runs 0 to 4, and a while loop tests before each pass — so it may run zero times. Trace the first and last passes explicitly.
IB-style questionConstruct[5 marks]

A program asks for marks until the user types -1, then prints how many were entered and how many were passes (50 or more). Write it.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on Loops. Write your answer and get instant AI feedback — just like a real IB examiner.

A program reads a number n once, then uses a counted loop to print n × 1, n × 2 and so on up to n × 12. what would happen if the input statement were moved inside the loop. [2 marks]

Related Computer Science Topics

Continue learning with these related topics from the same unit:

6.1.1Variables and data types6.1.2Substrings6.1.3Exception handling6.1.4Debugging
View all Computer Science topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science
Previous6.3.2SelectionNextFunctions and modularity6.3.4

58 exam-style questions ready for you

Students who practice on Aimnova improve their scores by 15% on average. Get instant feedback that shows exactly how to improve your answers.

Practice Now — FreeView All Computer Science Topics