A Problem I've had for a "while"

Hey guys, this is a recurring problem I've had whenever I try to use the while loop. It says it expects a ';' before a parenthesis but it doesn't need one. I'll post the code it's happening in below.

1
2
3
4
5
6
7
8
9
10
int main()
{
    int X = 10;

    while(X; X <= 0; X--)
        cout << X << endl;


    return 0;
}


The exact error is:
"Error: expected ')' before ';' token"
and
"Error: expected ';' before ')' token"
on line 11 (line 5 here).


Any help would be appreciated. And I've also got a question regarding some math I need help with but have yet to write in the code.
Last edited on
You are trying to use a for loop:

1
2
3
4
5
6
7
8
9
10
int X = 10;

    while(X != 0) // <== This is how a while loop works.
    {
        cout << X << endl;
        X--;
    }

    for(X; X >= 0; X--) // <== You are trying to do this! ( Just change your while to for )
        cout << X << endl;

Also I changed your <= to >= ( in the for loop )

!= means "not equal to"
Last edited on
In addition to what he said, if you really wants to do with while loop, you should do something like that:

1
2
3
4
5
6
7
8
9

int x = 10;
while(x <= 0) {

    cout << x << endl;
    x--;

}
Last edited on
Registered users can post here. Sign in or register to post.