continue skips to the next loop pass, while break stops the loop.

break `break` exits the nearest loop immediately.

Break and Continue

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

int main() {
    int stopAt = 8;
    int values[6] = {2, -1, 4, 6, -3, 8};
    int total = 0;

    for (int value : values) {
        if (value < 0) {
            continue;
        }
        if (value == stopAt) {
            break;
        }
        total += value;
    }

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

int main() {
    int stopAt = 6;
    int values[6] = {2, -1, 4, 6, -3, 8};
    int total = 0;

    for (int value : values) {
        if (value < 0) {
            continue;
        }
        if (value == stopAt) {
            break;
        }
        total += value;
    }

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

int main() {
    int stopAt = 10;
    int values[6] = {2, -1, 4, 6, -3, 8};
    int total = 0;

    for (int value : values) {
        if (value < 0) {
            continue;
        }
        if (value == stopAt) {
            break;
        }
        total += value;
    }

    std::cout << "stopAt=" << stopAt << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
  1. stopAt ← 8, values ← ⟨addr A⟩, total ← 0

    3int main() {4    int stopAt→ 8 = 8; //@stopAt=6, 105    int values→ ⟨addr A⟩[6] = {2, -1, 4, 6, -3, 8};6    int total→ 0 = 0;
  2. total ← 2

    pass 1 of 6
    8for (int value2 : values⟨addr A⟩) {9    if (value < 0) {10        continue;11    }12    if (value == stopAt) {13        break;14    }15    total→ 2 += value2;16}
    All 6 passes — pass 1 is the card above
    passvaluestopAttotal
    120 2
    2-1
    342 6
    466 12
    5-3
    688
  3. if (value < 0)

    pass 1 of 2
    8for (int value : values) {9    if (value-1 < 0) {10        continue;11    }
  4. if (value < 0)

    pass 2 of 2
    8for (int value : values) {9    if (value-3 < 0) {10        continue;11    }
  5. if (value == stopAt)

    11}12if (value8 == stopAt8) {13    break;14}
  6. std::cout << "stopAt=" << stopAt << std::endl;

    18    std::cout << "stopAt=" << stopAt8 << std::endl;19    std::cout << "total=" << total12 << std::endl;20    return 0;21}
    outputstopAt=8
    total=12
  1. stopAt ← 6, values ← ⟨addr A⟩, total ← 0

    3int main() {4    int stopAt→ 6 = 6;5    int values→ ⟨addr A⟩[6] = {2, -1, 4, 6, -3, 8};6    int total→ 0 = 0;
  2. total ← 2

    pass 1 of 4
    8for (int value2 : values⟨addr A⟩) {9    if (value < 0) {10        continue;11    }12    if (value == stopAt) {13        break;14    }15    total→ 2 += value2;16}
    All 4 passes — pass 1 is the card above
    passvaluestopAttotal
    120 2
    2-1
    342 6
    466
  3. if (value < 0)

    8for (int value : values) {9    if (value-1 < 0) {10        continue;11    }
  4. if (value == stopAt)

    11}12if (value6 == stopAt6) {13    break;14}
  5. std::cout << "stopAt=" << stopAt << std::endl;

    18    std::cout << "stopAt=" << stopAt6 << std::endl;19    std::cout << "total=" << total6 << std::endl;20    return 0;21}
    outputstopAt=6
    total=6
  1. stopAt ← 10, values ← ⟨addr A⟩, total ← 0

    3int main() {4    int stopAt→ 10 = 10;5    int values→ ⟨addr A⟩[6] = {2, -1, 4, 6, -3, 8};6    int total→ 0 = 0;
  2. total ← 2

    pass 1 of 6
    8for (int value2 : values⟨addr A⟩) {9    if (value < 0) {10        continue;11    }12    if (value == stopAt) {13        break;14    }15    total→ 2 += value2;16}
    All 6 passes — pass 1 is the card above
    passvaluetotal
    120 2
    2-1
    342 6
    466 12
    5-3
    6812 20
  3. if (value < 0)

    pass 1 of 2
    8for (int value : values) {9    if (value-1 < 0) {10        continue;11    }
  4. if (value < 0)

    pass 2 of 2
    8for (int value : values) {9    if (value-3 < 0) {10        continue;11    }
  5. std::cout << "stopAt=" << stopAt << std::endl;

    18    std::cout << "stopAt=" << stopAt10 << std::endl;19    std::cout << "total=" << total20 << std::endl;20    return 0;21}
    outputstopAt=10
    total=20

Skip Negatives, Stop at Match

  1. The loop visits 2, -1, 4, 6, -3, and 8.
  2. Negative values use continue, so they are skipped.
  3. When the loop reaches 8, break stops before adding it.
  4. The total is 2 + 4 + 6, which is 12. | Value | Action | Running total | | --- | --- | --- | | 2 | add | 2 | | -1 | continue | 2 | | 4 | add | 6 | | 6 | add | 12 | | -3 | continue | 12 | | 8 | break | 12 |

Exercise: break_continue.cpp

Skip negative values with continue, stop at a target with break, and print the total