Nexus Learn

Lecture 14: Break Continue Loop Patterns

Lecture Slides CS-307 Programming Fundamentals 7 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Slide 2

Quick Recap of Lecture 13

5 minutes · homework check

Homework 4

  • double sum = 0;
  • for (int i = 1; i <= 10; i++) {
  • sum += 1.0 / i; // not 1 / i !
  • }
  • // Sum = 2.92897
  • Today's question
  • You are searching a class register for one name. You found it on page 3. Do you keep reading all 40 pages?
  • The attendance list has absent students. Do you add their (missing) marks to the total?

Recap

Programming Fundamentals · Lecture 14

Slide 3

Today's Plan

50 minutes · 8 parts

Recap & homework check

5 min

Learning outcomes & two questions

3 min

break: leave the loop now

9 min

continue: skip to the next round

8 min

Five loop patterns

14 min

Class activity

7 min

Traps & recap quiz

3 min

Homework & next lecture

1 min

Programming Fundamentals · Lecture 14

Slide 4

Learning Outcomes (SLOs)

By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)

Explain how break and continue change the flow of a loop

SLO 14.1 → CLO4 · C2

Use break and continue correctly in while and for loops

SLO 14.2 → CLO4 · C3

SLO 14.3 → CLO1 · C3

Apply the standard loop patterns: count, sum, maximum/minimum, flag and search

  • CLO1 Identify the necessary properties of good problem-solving techniques.
  • CLO4 Describe the syntax and semantics of the C++ programming language.

Programming Fundamentals · Lecture 14

Slide 5

break: Leave the Loop Now

Jump straight to the first statement after the loop

Shape

  • What happens
  • The rest of this round is skipped.
  • No more rounds are run.
  • The update (i++) of the for loop does not run either.
  • The program continues after the loop's }.
  • for (…; …; …) {
  • …
  • if (found) {
  • break; // leave now
  • }
  • …
  • }
  • // continues here
  • Real-world
  • Searching for your roll number on the result notice board: once you find it, you stop reading the list.

Module 1 · break

Programming Fundamentals · Lecture 14

Slide 6

Example: First Multiple of 7 between 50 and 100

Stop as soon as the answer is found

break_find.cpp (main part)

Output

  • for (int n = 50; n <= 100; n++) {
  • if (n % 7 == 0) {
  • cout << "First multiple of 7: " << n << endl;
  • break; // stop the loop now
  • }
  • }
  • cout << "After the loop" << endl;
  • First multiple of 7: 56
  • After the loop

Only 7 rounds instead of 51 — break saves work when the answer is found early.

Module 1 · break

Programming Fundamentals · Lecture 14

Slide 7

while (true) with break

A loop that only ends from the inside

menu_true.cpp

Session

  • int choice;
  • while (true) { // ends only with break
  • cout << "1. Hello 2. Exit: ";
  • cin >> choice;
  • if (choice == 2) {
  • break;
  • }
  • if (choice == 1) {
  • cout << "Assalam-o-Alaikum!" << endl;
  • }
  • }
  • cout << "Program ended." << endl;
  • 1. Hello 2. Exit: 1
  • Assalam-o-Alaikum!
  • 1. Hello 2. Exit: 1
  • Assalam-o-Alaikum!
  • 1. Hello 2. Exit: 2
  • Program ended.
  • Compare
  • Lecture 12 wrote menus with do-while. Both work; pick the one that reads more clearly.

Module 1 · break

Programming Fundamentals · Lecture 14

Slide 8

continue: Skip to the Next Round

Skip the rest of THIS round, but keep looping

continue_skip.cpp (main part)

  • for (int i = 1; i <= 10; i++) {
  • if (i % 3 == 0) {
  • continue; // skip this round
  • }
  • cout << i << " ";
  • }

Output

  • Real-world
  • A teacher marking papers skips a blank answer sheet and moves on to the next one.

1 2 4 5 7 8 10

Module 2 · continue

Programming Fundamentals · Lecture 14

Slide 9

break and continue Together: Attendance Marks

6 students · -1 = absent (skip) · 999 = stop early

attendance.cpp (loop part)

Input and output

  • int marks, total = 0, present = 0;
  • for (int i = 1; i <= 6; i++) {
  • cin >> marks;
  • if (marks == 999) {
  • break;
  • }
  • if (marks == -1) {
  • continue;
  • }
  • total += marks;
  • present++;
  • }
  • 70 -1 85 64 999 90
  • Present: 3, total: 219
  • Average: 73

Order matters: check the stop value first, then the skip value.

Module 2 · continue

Programming Fundamentals · Lecture 14

Slide 10

Five Patterns Inside Almost Every Loop

Learn the shape once, reuse it everywhere

Real-world: a cricket scorer counts boundaries, sums runs, notes the highest score and checks if anyone made a century.

Module 3 · Five Loop Patterns

Programming Fundamentals · Lecture 14

Slide 11

All Five in One Program: A Week of Temperatures

Seven daily temperatures (example values)

patterns.cpp (loop part)

Input and output

  • int count = 0, total = 0, highest, lowest;
  • bool anyBelow15 = false; // flag
  • for (int day = 1; day <= 7; day++) {
  • int t;
  • cin >> t;
  • total += t; // sum
  • if (t > 30) count++; // count
  • if (day == 1 || t > highest) highest = t; // maximum
  • if (day == 1 || t < lowest) lowest = t; // minimum
  • if (t < 15) anyBelow15 = true; // flag
  • }
  • 28 33 31 14 25 35 22
  • Days above 30: 3
  • Average : 26.8571
  • Highest : 35
  • Lowest : 14
  • Any day below 15? yes
  • Flag
  • A bool that starts false and becomes true the first time something happens.

Module 3 · Five Loop Patterns

Programming Fundamentals · Lecture 14

Slide 12

Maximum & Minimum: Start From the First Value

Starting the maximum at 0 fails for negative numbers

max_zero_bug.cpp

Fixed: start from the first value

  • int highest = 0; // wrong start!
  • for (int i = 1; i <= 4; i++) {
  • int t;
  • cin >> t;
  • if (t > highest) highest = t;
  • }
  • int highest;
  • for (int i = 1; i <= 4; i++) {
  • int t;
  • cin >> t;
  • if (i == 1 || t > highest)
  • highest = t;
  • }

Input and output — wrong (should be -1)

  • Rule
  • Max/min start with the first value. Count/sum start at 0. Product starts at 1.
  • -3 -7 -1 -5
  • Highest: 0

Module 3 · Five Loop Patterns

Programming Fundamentals · Lecture 14

Slide 13

CLASS ACTIVITY · 7 MIN · IN PAIRS

Use the Right Tool

Write the loop for each task. Say which keyword or pattern you used.

Tasks

Hints

  • 1. Find the first n from 1 to 100 whose square is more than 500.
  • 2. Add the numbers 1 to 20 but skip multiples of 4.
  • 3. Read 5 numbers; count how many are even.
  • 4. Read 5 marks; print "Someone failed" if any mark is below 50.
  • First … → search + break
  • Skip … → continue
  • How many … → count
  • Any … → flag

Slide 14

Traps

continue in a while loop, and break in nested loops

continue_while_bug.cpp

  • 1 continue skips the update
  • In while loops, update BEFORE continue — or use for.
  • int i = 1;
  • while (i <= 5) {
  • if (i == 3) {
  • continue; // skips i++!
  • }
  • cout << i << " ";
  • i++;
  • }
  • 2 break leaves one loop
  • Only the innermost loop (Lecture 15).

Output

1 2 (then the program hangs)

  • 3 Use break wisely
  • Many breaks = hard to read.

Programming Fundamentals · Lecture 14

Slide 15

Quick Recap

Answer aloud

Q1

What is the difference between break and continue?

Q2

In a for loop, does i++ run after continue? After break?

Q3

Why must a maximum not start at 0?

Q4

Which pattern answers 'Is there any …?'

Programming Fundamentals · Lecture 14

Slide 16

Homework

Read numbers until 0; print how many were positive and how many negative (count pattern).

Print the first 5 numbers between 100 and 200 that are divisible by both 3 and 7 (use a counter and break).

Read 10 marks; skip any outside 0–100 with continue; print the highest and lowest valid marks.

Rewrite attendance.cpp with a while loop. Where must the counter be updated?

Next lecture: Nested loops — a loop inside a loop (tables and patterns)

← All CS-307 material