C++ Lesson 17: The ‘Remote Control’ in Loops

C++ Lesson 17: The 'Remote Control' in LoopsHello everyone, I am Teacher Geng, teaching programming. This is the 17th lesson in our C++ programming series, welcome back!In the previous lessons, we have learned to write various loops using <span>for</span> and <span>while</span>. However, in actual programming, loops do not always need to run to completion.Today, we will learn two keywords that are used to interrupt or skip loops:<span>break —— terminate the loop</span><span>continue</span> —— skip the current iteration of the loopFirst, let’s look at<span><span>break</span></span>: immediately terminate the entire loopYou can think of it as:The teacher slams the desk, everyone stop writing, homework is over!Let’s look at an example: output numbers starting from 1, and stop immediately when reaching 5

#include <iostream>
using namespace std;
int main(){
    for(int i = 1; i <= 10; i++){
        if(i == 5){
            break;
        }
        cout << i << " ";
    }
    return 0;
}

Output:

1 2 3 4

When <span>i == 5</span> is triggered, <span>break</span> causes the entire loop to end early, and the subsequent numbers 6, 7, 8, 9, 10 are not output.

Next, let’s look at<span><span>continue</span></span>: skip this iteration and continue to the next one

You can think of it as:The teacher sees you got this question wrong, takes it away, and you continue with the next question.

Let’s see an example: output the odd numbers from 1 to 10 (skip the even numbers)

#include <iostream>
using namespace std;
int main(){
    for(int i = 1; i <= 10; i++){
        if(i % 2 == 0){
            continue;
        }
        cout << i << " ";
    }
    return 0;
}

Output:

1 3 5 7 9

When <span>i</span> is even, it triggers <span>continue</span>, skipping the current iteration and not executing <span>cout</span>.

To summarize:

Statement Used Purpose Illustration Recommended Scenario
<span>break</span> leave class early exit the loop immediately after finding the answer
<span>continue</span> “Forget this question, skip!” filter out and skip non-compliant situations

Very simple, right? Really very simple.

Let’s do a practice problem: output the first 10 numbers from 1 to 100 that are not multiples of 7.

Requirements:

Skip numbers that are divisible by 7 (use <span>continue</span>)

Stop printing after reaching 10 numbers (use <span>break</span>)

#include <iostream>
using namespace std;
int main(){
    int count = 0;
    for(int i = 1; i <= 100; i++){
        if(i % 7 == 0){
            continue;
        }
        cout << i << " ";
        count++;
        if(count == 10){
            break;
        }
    }
    return 0;
}

Output:

1 2 3 4 5 6 8 9 10 11

This problem is purely to demonstrate the use of <span>continue</span> and <span>break</span>, and normally you wouldn’t write such clumsy code. To exit at 10, you could just use <span>i <= 10</span> in the for loop, right? Haha.

Alright, let’s summarize again:

Keyword Function Analogy Understanding
<span>break</span> end the entire loop the teacher shouts stop, the whole class stops writing homework
<span>continue</span> skip the current iteration, continue to the next not writing this question, continue to the next

Okay, that’s all for this lesson!

Congratulations on completing Lesson 17 of C++! If you have any questions, feel free to leave a comment.

If you have learned it, please leave a comment saying “Got it”, and if you find it useful, share it with your friends to learn together.See you in the next lesson.

Enrollment Guidelines:

Meet the following conditions:

1. Fourth grade or above, with adequate math skills.

2. Pass the entrance math assessment.

3. No pressure in class learning, with ample time outside of class.

C++ Lesson 17: The 'Remote Control' in LoopsC++ Easy Introduction Tutorial:

001 Write your first line of C++ code
002 How to output a new line
003 Expressions, Operators, and Variables
004 Variable Naming Rules and Input
005 Compound Operations and Increment/Decrement
006 Number Decomposition
007 Data Type: float
008 Single Branch and Double Branch
009 Multiple Branch Structures
010 Logical Operators
011 Finding Common Multiples and Maximum Value
012 In-depth Understanding of switch Statement
013 while Loop
014 Characters and ASCII Codes
015 do…while Loop
016 for Loop

Leave a Comment