UNIVERSITY OF AGRICULTURE, FAISALABAD
Department of Computer Science
LECTURE 14 · STUDENT NOTES
break, continue & Common Loop Patterns
CS-307 Programming Fundamentals · BSCS Semester 1
Week | Slides | Linked practical | Reference ID |
6 | Lecture14_Break_Continue_Loop_Patterns.pptx | Practical 9 | N14 |
Learning Outcomes
SLO | By the end of this lecture you can… | Builds CLO | Bloom level |
|---|---|---|---|
SLO 14.1 | Explain how break and continue change the flow of a loop | CLO4: Describe the syntax and semantics of the C++ programming language. | C2 |
SLO 14.2 | Use break and continue correctly in while and for loops | CLO4: Describe the syntax and semantics of the C++ programming language. | C3 |
SLO 14.3 | Apply the standard loop patterns: count, sum, maximum/minimum, flag and search | CLO1: Identify the necessary properties of good problem-solving techniques. | C3 |
How to use these notes Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste. |
1. Changing a Loop from the Inside
So far a loop stopped only when its condition became false. Two keywords change the flow from inside the body: break (leave the loop now) and continue (skip the rest of this round). Real-world: when you find your roll number on a notice board you stop reading (break); when a teacher meets a blank answer sheet she moves on to the next one (continue).
2. break
2.1 What break does
(Slide L14-S05)
- The rest of the current round is skipped.
- No more rounds run — even if the condition is still true.
- In a for loop the update (i++) does not run.
- The program continues with the first statement after the loop.
2.2 Example: first multiple of 7
(Slide L14-S06)
break_find.cpp #include <iostream> using namespace std;
int main() { // first number from 50 to 100 that is divisible by 7 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; return 0; } |
Output First multiple of 7: 56 After the loop |
The loop stops after 7 rounds (50 … 56) instead of 51. Without break it would print every multiple: 56 63 70 77 84 91 98.
2.3 while (true) with break
while (true) never stops by itself; the only way out is break. It is a common shape for menus. (Slide L14-S07)
menu_true.cpp #include <iostream> using namespace std;
int main() { 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; return 0; } |
Session 1. Hello 2. Exit: 1 Assalam-o-Alaikum! 1. Hello 2. Exit: 1 Assalam-o-Alaikum! 1. Hello 2. Exit: 2 Program ended. |
Careful Always make sure that some path reaches the break, or the program never ends. |
3. continue
3.1 What continue does
continue skips the rest of the current round and goes on to the next one. In a for loop the update still runs, then the condition is checked. The loop does not end. (Slide L14-S08)
continue_skip.cpp #include <iostream> using namespace std;
int main() { // print 1 to 10 but skip multiples of 3 for (int i = 1; i <= 10; i++) { if (i % 3 == 0) { continue; // skip this round } cout << i << " "; } cout << endl; return 0; } |
Output — 3, 6 and 9 are skipped 1 2 4 5 7 8 10 |
break | continue | |
|---|---|---|
Rest of this round | skipped | skipped |
Later rounds | none | still run |
for loop update | does not run | runs |
3.2 break and continue together: attendance
Six students; -1 means absent (skip the student), 999 means stop early. (Slide L14-S09)
attendance.cpp #include <iostream> using namespace std;
int main() { // marks of 6 students; -1 means absent (skip), 999 means stop early 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++; } cout << "Present: " << present << ", total: " << total << endl; if (present > 0) { cout << "Average: " << static_cast<double>(total) / present << endl; } return 0; } |
Input and output 70 -1 85 64 999 90 Present: 3, total: 219 Average: 73 |
Input | 70 | -1 | 85 | 64 | 999 | 90 |
|---|---|---|---|---|---|---|
Action | add | continue | add | add | break | never read |
4. Five Loop Patterns
A pattern is a ready-made way to solve a common small problem. These five appear in almost every loop program. (Slide L14-S10)
Pattern | Question | Before the loop | Inside the loop |
|---|---|---|---|
Count | How many …? | count = 0 | if (rule) count++; |
Sum | What is the total / average? | sum = 0 | sum += x; |
Maximum / minimum | Which is the largest / smallest? | first value | if (x > max) max = x; |
Flag | Is there ANY …? | found = false | if (rule) found = true; |
Search | Where is the first …? | — | if (rule) { …; break; } |
4.1 All five in one program
Seven daily temperatures (example values). (Slide L14-S11)
patterns.cpp #include <iostream> using namespace std;
int main() { // temperatures of 7 days in Burewala (example values) 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 pattern if (t > 30) count++; // count pattern if (day == 1 || t > highest) highest = t; // maximum pattern if (day == 1 || t < lowest) lowest = t; // minimum pattern if (t < 15) anyBelow15 = true; // flag pattern } cout << "Days above 30: " << count << endl; cout << "Average : " << total / 7.0 << endl; cout << "Highest : " << highest << endl; cout << "Lowest : " << lowest << endl; cout << "Any day below 15? " << (anyBelow15 ? "yes" : "no") << endl; return 0; } |
Input and output 28 33 31 14 25 35 22 Days above 30: 3 Average : 26.8571 Highest : 35 Lowest : 14 Any day below 15? yes |
4.2 Maximum and minimum must start from the first value
(Slide L14-S12)
max_zero_bug.cpp #include <iostream> using namespace std;
int main() { // highest of 4 winter night temperatures (all below zero) int highest = 0; for (int i = 1; i <= 4; i++) { int t; cin >> t; if (t > highest) highest = t; } cout << "Highest: " << highest << endl; return 0; } |
Input and output — wrong (should be -1) -3 -7 -1 -5 Highest: 0 |
Zero is never beaten by a negative value, so a number that was never entered is printed. Start from the first value: if (i == 1 || t > highest) highest = t;
Starting values Count / sum: 0. Product: 1. Maximum / minimum: the first value. Flag: false. |
5. Traps
(Slide L14-S14)
continue_while_bug.cpp #include <iostream> using namespace std;
int main() { int i = 1; while (i <= 5) { if (i == 3) { continue; // skips i++! } cout << i << " "; i++; } return 0; } |
Output 1 2 (then the program hangs) |
- continue in a while loop jumps over the update written at the end of the body, so i stays 3 for ever. Update before continue — or use a for loop, whose update always runs.
- break leaves only the innermost loop. With nested loops (Lecture 15) the outer loop keeps running.
- Too many breaks make code hard to follow; prefer a clear loop condition when possible.
6. Word Bank (Key Terms)
Word | Simple meaning |
|---|---|
break | Leaves the loop immediately |
continue | Skips the rest of the current round |
while (true) | A loop that only ends with break |
Pattern | A ready-made way to solve a common small problem |
Count pattern | Counts items that match a rule |
Flag | A bool that records whether something has happened |
Search pattern | Stops at the first item that matches |
7. Self-Check Questions
1. What is the difference between break and continue?
2. In a for loop, does the update run after continue? After break?
3. Write a loop that prints the first number from 1 to 100 whose square is more than 500.
4. Add 1 to 20 but skip multiples of 4. What is the result?
5. Why must a maximum start from the first value?
6. Why does continue_while_bug.cpp hang?
Answers: §2–3; §3.1; 23; 150; §4.2; §5. Homework is on Slide L14-S16.