A while loop repeats while its condition remains true.

while A `while` loop checks the condition before each pass through the loop body.

While Loop

start
while_loop.cpp
Replay: real traced execution (multi-file project)
#include <iostream>

int main() {
    int start = 3;
    int current = start;
    int total = 0;

    while (current > 0) {
        total += current;
        current--;
    }

    std::cout << "start=" << start << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int start = 1;
    int current = start;
    int total = 0;

    while (current > 0) {
        total += current;
        current--;
    }

    std::cout << "start=" << start << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int start = 5;
    int current = start;
    int total = 0;

    while (current > 0) {
        total += current;
        current--;
    }

    std::cout << "start=" << start << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
  1. start ← 3, current ← 3, total ← 0

    3int main() {4    int start→ 3 = 3; //@start=1, 55    int current→ 3 = start3;6    int total→ 0 = 0;
  2. total ← 3, current ← 2

    pass 1 of 3
    8while (current3 > 0) {9    total→ 3 += current3;10    current→ 2--;11}
    All 3 passes — pass 1 is the card above
    passtotalcurrent
    10 33 2
    23 52 1
    35 61 0
  3. std::cout << "start=" << start << std::endl;

    13    std::cout << "start=" << start3 << std::endl;14    std::cout << "total=" << total6 << std::endl;15    return 0;16}
    outputstart=3
    total=6
  1. start ← 1, current ← 1, total ← 0

    3int main() {4    int start→ 1 = 1;5    int current→ 1 = start1;6    int total→ 0 = 0;
  2. total ← 1, current ← 0

    8while (current1 > 0) {9    total→ 1 += current1;10    current→ 0--;11}
  3. std::cout << "start=" << start << std::endl;

    13    std::cout << "start=" << start1 << std::endl;14    std::cout << "total=" << total1 << std::endl;15    return 0;16}
    outputstart=1
    total=1
  1. start ← 5, current ← 5, total ← 0

    3int main() {4    int start→ 5 = 5;5    int current→ 5 = start5;6    int total→ 0 = 0;
  2. total ← 5, current ← 4

    pass 1 of 5
    8while (current5 > 0) {9    total→ 5 += current5;10    current→ 4--;11}
    All 5 passes — pass 1 is the card above
    passtotalcurrent
    10 55 4
    25 94 3
    39 123 2
    412 142 1
    514 151 0
  3. std::cout << "start=" << start << std::endl;

    13    std::cout << "start=" << start5 << std::endl;14    std::cout << "total=" << total15 << std::endl;15    return 0;16}
    outputstart=5
    total=15

Count Down to Zero

  1. current starts at 3.
  2. The loop runs while current > 0.
  3. Each pass adds current to total.
  4. Each pass subtracts 1 from current.
  5. The final total is 6. | Current | Running total | | --- | --- | | 3 | 3 | | 2 | 5 | | 1 | 6 |

Exercise: while_loop.cpp

Use a while loop to count down from a start value and add the running total