continue Statement

closed account (EwCjE3v7)
The code acts differently to the way I suspected it to.

It prints
Test-2
though I don't know why, Why does this loop end? Should it keep executing, but some how control goes outside the loop. Why?

1
2
3
4
5
6
7
8
9
10
int main()
{
    while (int sz = 0) { // true
        if (sz <= 0)
            continue; // goes back to condtion, right?
        cout << "Test-1";
    
    cout << "Test-2";
    return 0;
}
I can't find the ending bracket of your while loop.
closed account (EwCjE3v7)
oh haha. (facepalm)*300

but still same results...
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    while (int sz = 0) {
        if (sz <= 0)
            continue;
        cout << "Test-1";
    }
    cout << "Test-2";
    return 0;
}


EDIT:

Unusual, on the C++ Shell, it says
Exit code: 0 (normal program termination)
but does not print anything and when I compile it with clang myself, Test-2 is printed.


User@Unknown ~/Testing $ clang++ -std=c++11 Testing.cpp -o t
User@Unknown ~/Testing $ ./t
Test-2User@Unknown ~/Testing $ 

Last edited on
It assigns 0 and then checks if the statement is true (not 0) so after creating the variable sz and assigning a value of 0 it checks if 0 is not equal to 1 which is false so it does not enter the loop.

Basically your code is equal to

1
2
3
4
5
6
7
8
while(false)
{
    //stuff that will never happen
}

cout << "Test-2";

return 0;
closed account (EwCjE3v7)
I'm going to give myself another 300 facepalms.

Thanks lads.
Topic archived. No new replies allowed.