'Break operator with continue c++ [closed]

My code:


using namespace std;

int main()
{
    int n; 
    int total = 0;
    cout << "Enter a value: ";
    cin >> n;
    while (total + n <= 1000)
    {
        cout << total << " ";
        total = n + total;
        continue;
    }
}

I need to add the following condition: if int n<0 program must close with a break operator



Solution 1:[1]

If you want to break out of the while loop you can use something like this:

while (total + n <= 1000)
    {
        cout << total << " ";
        total = n + total;
        if (n < 0) break;
    }

But because variable n does not change in the loop I suggest you use return before the while loop:

int n; 
int total = 0;
cout << "Enter a value: ";
cin >> n;
if (n < 0) return 0;
while(...){
...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ͔̲̲͛́̑̑̒̅ͅ ̴͈̮͉͉͛̀̒͋̒ ̲̎̅̽̑̀̿