If statements not working properly.

Now it's been almost a year since I was last doing programming, plus my already beginner level skills leaves me at a low point here. But I'm pretty sure if statements were easy at one point, however I seem to have forgotten them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void stuff()
{
	int x = 0;
	while(!GetAsyncKeyState(35))
	{
		if(x > 100)
			x - x;
		else if (x == 101)
			x + 1;
		else
			x++;

		printf("X = %i\n", x);
		Sleep(100);
	}
}


As you can see it is a loop, and when I run this loop my output counts up as it should. However, when the count reaches 101 it stop counting. It continues to print new lines, so the loop does continue to run. But it does not restart counting or move on to 102.

I'm sorry if I'm missing something obvious, but whatever it is, I'm not seeing it and I've been staring at this code for about four or five hours.


P.S. I am sorry if this is in the wrong forum/topic.
Last edited on
well, your x - x; and x + 1; do nothing.
you must assign outcome of operation to something:
x = x - x; and x = x + 1;

Also:
you if is created wrong. If 1st condition is true, program wont check any else conditions. And in your case 1st condition is true when 2nd is, so else if wont be ever executed. You can change place of if and else if for workaround:

1
2
3
4
5
6
7
8
9
10
11
12
	while(!GetAsyncKeyState(35))
	{
		if (x == 101)
			x = x + 1;
		else if (x > 100)
			x = x - x;
		else
			x++;

		printf("X = %i\n", x);
		Sleep(100);
	}
I realized the first condition is counter-intuitive, but I was curious if it was just skipping it all together, but it was not. I missed the obvious. Setting the expressions equal to something.
Setting the expressions equal to something.

At the risk of sounding pedantic, that's not quite the way to describe it. The = operator isn't really to do with equality. It's the assignment operator. What is happening is that the result of the expression to the right of the '=' operator is assigned to (or stored in) the variable to the left.
Topic archived. No new replies allowed.