BREAK and CONTINUE statements in C++

In this tutorial, we will learn two very important statements, or commands: break and continue in C++.

Statement BREAK in C++

If you did well and in order, the Progressive C++ course, you should already know this break command, because we used in the tutorial:
Switch, case and break

There, when this command was executed, it simply terminated the SWITCH conditional test.
Here he does the same thing, but in this case he serves to break a loop at any moment.

In the code below, C ++ keeps asking the user for a number and calculating its square.

If at any time the user enters 0, the IF becomes true and the break command is triggered, briefly breaking the WHILE loop:
#include <iostream>
using namespace std;

int main()
{
    int num;

    while(true){
        cout<<"Number: ";
        cin >>num;

        if(num==0)
            break;

        cout<<num*num<<endl;
    }

    return 0;
}
That is, when we want to break a loop at some point, we use the BREAK statement, which usually comes within a conditional test, within some looping.

If we have one loop inside another, and inside that is a BREAK statement, only that innermost loop will be broken, okay?

The following code keeps asking the user for grades to average.

If you enter a note that is not valid (below 0 or above 10), the WHILE loop ends and we provide the average of the numbers entered for that invalid note:

#include <iostream>
using namespace std;

int main()
{
    int aux=0;
    float num, sum=0;

    while(true){
        cout<<"Grade: ";
        cin >> num;

        if(num<0 || num>10)
            break;

        sum+=num;
        aux++;
    }

    cout<<"Average: "<<(sum/aux)<<endl;
    return 0;
}
Note that we guarantee that the numbers provided are correct (between 0 and 10), otherwise the loop will end.

The CONTINUE statement in C++

If BREAK terminates the conditional test or repetition statement, the CONTINUE command terminates the iteration only.

That is, skips to the next iteration.

Let's sum all numbers from 1 to 100 except multiples of 4:
#include <iostream>
using namespace std;

int main()
{
    int num, sum=0;

    for(num=1; num<=10 ; num++){
        if(num%4==0)
            continue;
        sum += num;
    }

    cout<<"Total: "<<sum<<endl;
    return 0;
}
Inside the loop we check if the number is divisible by 4, if so, this iteration is skipped, not running the code following CONTINUE, goes to the next iteration normally.

Like BREAK, the CONTINUE command usually occurs under a certain conditional test when you want to exclude a specific looping iteration.

Let's redo the code that averages.

Now, instead of stopping execution, it skips the iteration, not adding that invalid note to the sum:
#include <iostream>
using namespace std;

int main()
{
    int aux=0;
    float num, sum=0;

    while(true){
        cout<<"Grade: ";
        cin >> num;

        if(num>10){
            cout<<"Grade above 10 is invalid"<<endl;
            continue;
        }
        if(num<0){
            cout<<"Negative grade, shutting down and displaying average: ";
            break;
        }

        sum+=num;
        aux++;
    }

    cout<<(sum/aux)<<endl;
    return 0;
}
Note that the average is calculated for valid numbers only. To end looping, you must enter a negative value.

No comments:

Post a Comment