I'm lost on this one..Any ideas?

Suppose that the input is 100 20 –8 50 20. What is the output of the following C++ code?

int sum = 0;
int num;
int j;

for (j = 1; j <= 4; j++)
{
cin >> num;
if (num < 0)
continue;

sum = sum + num;
}

cout << sum << endl;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int sum = 0;
int num;
int j;

int main()
{
    for (j = 1; j <= 4; j++)
    {
        cin >> num;
        if (num < 0)
            continue; // If num isn't negative, it'll continue, which adds these numbers together


        std::cout << "Number wasn't negative" << std::endl;
        sum = sum + num;
    }

    cout << sum << endl;
}


You may run this code and try to figure out what the continue; does.
Topic archived. No new replies allowed.