Why does this for loop not work as it's supposed to?

The initial value for x is 3, but the condition is for x to be 4. Why does it pass as true and loop endlessly, while it is clearly supposed to be false and not output anything? It looks like the problem is present whenever I put for x to be equal to ANY number as the condition. In the meantime x > 4 & x >= 4 work right (do not output anything) and x < 4 & x <= 4 do, too (output X once and twice respectively).
I feel like if I don't get to the bottom of this I won't be able to sleep at night.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>

using namespace std;

int main()
{
    for (int x = 3; x = 4; x++){
        cout << "X" << endl;
    }

return 0;

}


EDIT: AAAlright, I just realized that I used '=' for 'equals', when it's supposed to be '=='. Solved.
Last edited on
Welcome, nice job finding it quickly.
If you turn your compiler warnings on, it should pick up on this sort of mistake, making it easier next time. Everyone does this one once in a while.
Topic archived. No new replies allowed.