Nexus Learn

Lecture 11: While Loop

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

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Why Programs Repeat
  3. 2. The while Loop
  4. 3. Counter Loops
  5. 4. Condition-Controlled Loops
  6. 5. Loop Traps
  7. 6. Word Bank (Key Terms)
  8. 7. Self-Check Questions

UNIVERSITY OF AGRICULTURE, FAISALABAD

Department of Computer Science

LECTURE 11 · STUDENT NOTES

Repetitive Statements I: the while Loop

CS-307 Programming Fundamentals · BSCS Semester 1

Week

Slides

Linked practical

Reference ID

5

Lecture11_While_Loop.pptx

Practical 8

N11

Learning Outcomes

SLO

By the end of this lecture you can…

Builds CLO

Bloom level

SLO 11.1

Explain why programs need repetition and how the condition of a while loop controls it

CLO4: Describe the syntax and semantics of the C++ programming language.

C2

SLO 11.2

Write a while loop with a start value, a condition and an update

CLO4: Describe the syntax and semantics of the C++ programming language.

C3

SLO 11.3

Trace a loop with a trace table and find infinite-loop and off-by-one mistakes

CLO1: Identify the necessary properties of good problem-solving techniques.

C4

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. Why Programs Repeat

There are three basic ways to control the order of statements: sequence (one after another), selection (choose a path — Week 4) and repetition (do it again). A loop runs the same block of code many times. Printing 1 to 1000 with cout would need 1000 lines; a loop needs four. (Slide L11-S05)

Daily task

What repeats

Tasbeeh

count a bead 33 times

Running track

run one lap, 4 times

Cashier

scan one item until the basket is empty

ATM

ask for the PIN again if it is wrong

2. The while Loop

2.1 Syntax and flowchart

A while loop checks its condition first. If it is true, the body runs, and then the program goes back to check the condition again. When the condition becomes false, the loop ends. If the condition is false at the very start, the body runs zero times. (Slide L11-S06)

Syntax

while (condition) {

// body: runs again and again while condition is true

}

// continues here when the condition is false

Figure 11.1 – Flowchart of a while loop (note the arrow going back)

Compare with if: an if checks once and may run once; a while checks, runs, checks again, runs again … until false.

2.2 The three parts of a counter loop

(Slide L11-S07)

count.cpp

#include <iostream>

using namespace std;

int main() {

int i = 1; // 1. start

while (i <= 5) { // 2. condition

cout << i << " ";

i++; // 3. update

}

cout << endl << "Loop finished, i = " << i << endl;

return 0;

}

Output

1 2 3 4 5

Loop finished, i = 6

Part

In count.cpp

Job

1 Start

int i = 1;

give the counter its first value

2 Condition

i <= 5

checked before every round

3 Update

i++;

change the counter so the loop can end

After the loop i is 6 — the first value that made the condition false.

3. Counter Loops

3.1 Tracing a loop

One round of the body is called an iteration. A trace table (Lecture 7) shows each check of the condition. (Slide L11-S08)

Round

i

i <= 5 ?

Printed

i after i++

1

1

true

1

2

2

2

true

2

3

3

3

true

3

4

4

4

true

4

5

5

5

true

5

6

—

6

false

loop ends

The condition is checked 6 times; the body runs 5 times.

3.2 The sum (accumulator) pattern

An accumulator is a variable that collects a running total. It must start at 0 before the loop. (Slide L11-S09)

sum_n.cpp

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter n: ";

cin >> n;

int i = 1, sum = 0;

while (i <= n) {

sum = sum + i;

i++;

}

cout << "Sum of 1 to " << n << " = " << sum << endl;

return 0;

}

Run 1

Enter n: 10

Sum of 1 to 10 = 55

Run 2

Enter n: 100

Sum of 1 to 100 = 5050

i

1

2

3

4

…

10

sum after round

1

3

6

10

…

55

Real-world: a shopkeeper adds each item to the bill — the bill is the accumulator.

3.3 Counter-controlled vs condition-controlled

(Slide L11-S10)

Counter-controlled

Condition-controlled

Know the number of rounds?

yes

no

Examples

print 1 to 100; add 1 to n; 33 beads

save until the goal; remove digits until none are left; ask PIN until correct

Typical condition

while (i <= n)

while (saved < GOAL)

Both need something inside the body that eventually makes the condition false.

4. Condition-Controlled Loops

4.1 Saving for a new phone

(Slide L11-S11)

savings.cpp

#include <iostream>

using namespace std;

int main() {

const int GOAL = 30000; // price of a new mobile phone

int perMonth, saved = 0, months = 0;

cout << "Saving per month (Rs.): ";

cin >> perMonth;

while (saved < GOAL) {

saved = saved + perMonth;

months++;

}

cout << "Months needed: " << months << endl;

cout << "Total saved : Rs. " << saved << endl;

return 0;

}

Run 1

Saving per month (Rs.): 5000

Months needed: 6

Total saved : Rs. 30000

Run 2

Saving per month (Rs.): 7000

Months needed: 5

Total saved : Rs. 35000

Danger

If the user types 0, saved never grows and the loop never ends. Lecture 12 shows how to reject bad input with do-while.

4.2 Counting and adding digits

Each round adds the last digit (n % 10) and removes it (n / 10). The loop stops when n becomes 0. (Slide L11-S12)

digits.cpp

#include <iostream>

using namespace std;

int main() {

int n, count = 0, sum = 0;

cout << "Enter a positive number: ";

cin >> n;

while (n > 0) {

sum = sum + n % 10; // add the last digit

n = n / 10; // remove the last digit

count++;

}

cout << "Digits: " << count << ", sum of digits: " << sum << endl;

return 0;

}

Output

Enter a positive number: 4072

Digits: 4, sum of digits: 13

n

n % 10

sum

count

4072

2

2

1

407

7

9

2

40

0

9

3

4

4

13

4

0

—

stop

5. Loop Traps

An infinite loop is a loop whose condition never becomes false. Stop a hung console program with Ctrl + C (or close its window). (Slide L11-S14)

5.1 Missing update

infinite.cpp

#include <iostream>

using namespace std;

int main() {

int i = 1;

while (i <= 5) {

cout << i << " ";

// i++ is missing: i stays 1 for ever

}

return 0;

}

Output

1 1 1 1 1 1 1 1 1 1 … (never stops)

5.2 Stray semicolon

semicolon.cpp

#include <iostream>

using namespace std;

int main() {

int i = 1;

while (i <= 5); // stray semicolon: empty loop body

{

cout << i << " ";

i++;

}

return 0;

}

The ; after while (i <= 5) is an empty body; i never changes and the program hangs with no output. g++ -Wall warns: this 'while' clause does not guard....

5.3 Off by one

offbyone.cpp

#include <iostream>

using namespace std;

int main() {

int i = 1;

while (i < 5) { // should be i <= 5

cout << i << " ";

i++;

}

cout << endl;

return 0;

}

Output — 5 is missing

1 2 3 4

Off-by-one: the loop runs one time too few (or too many). Check the last value with a trace table: should it be < or <=?

6. Word Bank (Key Terms)

Word

Simple meaning

Loop / repetition

Running the same code many times

while loop

Repeats while its condition is true; checks first

Iteration

One round of the loop body

Counter

A variable that counts the rounds (often i)

Accumulator

A variable that collects a total; starts at 0

Counter-controlled loop

The number of rounds is known in advance

Condition-controlled loop

Repeats until something happens

Infinite loop

A loop that never stops

Off-by-one error

The loop runs one time too many or too few

7. Self-Check Questions

1. Draw the flowchart of a while loop. Which arrow makes it a loop?

2. Name the three parts of a counter loop in count.cpp.

3. How many times does the body run if the condition is false at the start?

4. What is i after: int i = 1; while (i <= 5) i++; ?

5. Write a loop that prints 10 9 8 … 1.

6. Give two causes of an infinite loop.

7. Trace digits.cpp for 905.

Answers are in §2.1, §2.2, §2.1, §2.2 (6), the Lecture 11 teaching guide, §5 and §4.2 (905: 3 digits, sum 14). Homework is on Slide L11-S16.

← All CS-307 material