Confused about a question of a basic code

n=1;
while (n<5)
{ n++;
cout<<n<<" ";
}

The output of the C++ code above is?

Idk why the answer is 2 3 4 5, not 2 3 4?
Another code

num = 100;
while (num <= 200)
num = num + 3;
cout << num;

Can anybody explain why the answer is 202 for me pls?
1
2
3
4
5
6
int n=1;
while (n<5)
{ 
    n++;
    cout << n << " ";
}

Execution of the program proceeds line by line in sequential order, starting at line 1. Loops and if statements can modify that order.

Here, n is set to the value 1 at line1.
at line 2, n is tested, is it less than 5?
Yes, 1 is less than 5.
the body of the loop is executed.
add 1 to n, it is now 2.
output the value of n.
on reaching the closing brace (line 6), execution jumps back to the start of the loop, at line 2.

is n less than 5? (yes, 2 is less than 5)
etc. etc.

is 4 yes than 5? yes.
add 1 to n, it is now 5.
output the value of n. (it is 5)
is n less than 5? No, so the loop terminates.
@tiara2009 If you don't mind me asking, why didn't you use a for loop for this code?
Thank you I got it now
How about the second code?
@the prince it's not my homework, it's a question from a quiz
Last edited on
Well, the second is very similar to the first. I was rather hoping that you could use the knowledge and method from understanding the first, and apply that in order to trace the second one for yourself.
You have to follow the code and what it does line by line.

1
2
3
4
num = 100;
 while (num <= 200)
 num = num + 3;
 cout << num;


So what this code is doing

first it sets num = 100

Then it checks is num <= 200. num = 100 and 100 is <= 200. The statement is true.

Because the statement is true it executes the body of the loop which is num = num + 3. Now num = 103.

It then goes back to check is 103 <=200. Again the answer is yes and it executed the body of the loop, and then check the Boolean expression again.

The loop will keep executing until the Boolean expression = false. The expression is false when num is >=200.

So if you follow the math.... the last execution is when num =199. 199 is still <= 200. The body executes again and num now = 203. Checks the Boolean and 203 <= 200 is false so it ends the loop and outputs 203.
Last edited on
but it doesn't have the bracket, it will be executed exactly the same way as it has bracket?
Last edited on
OK, let's discuss the bracket.

A while loop in general has this structure:
1
2
    while (condition)
        statement;

I think you what this means. First the condition is tested. If it is true, the statement is executed. After that, the condition is tested again and so on.

Notice that there is just a single statement controlled by the loop. Now it is a general principle in C++, that anywhere that you can use a single statement, you can replace that by a compound statement.

What is a compound statement? It consists of zero or more statements enclosed in braces. Some examples. First a single statement:
 
    cout << "a = " << a << endl; 

Now a compound statement:
1
2
3
    {
        cout << "a = " << a << endl; 
    }

Another:
 
    { } // empty compound statement 

and one more, a complete example this time:
1
2
3
4
5
6
7
8
    
    int a = 5;
    
    while (a--) 
    {
        cout << "a = " << a;
        cout << " squared = " << a * a << endl;
    }

Output:
a = 4 squared = 16
a = 3 squared = 9
a = 2 squared = 4
a = 1 squared = 1
a = 0 squared = 0


But - I'm just reciting the contents of a textbook. Rather than continuing to do that, I would strongly suggest that you read through the tutorial pages on this site:
http://www.cplusplus.com/doc/tutorial/

And even better, get yourself a C++ textbook as well.
Topic archived. No new replies allowed.