Ignoring Code?

This was prompted when I wanted to try a 1-button toggle.

When I went to reset a variable via an “If” statement, it completely ignored it and kept right on adding. I have tried this in the simplest format, and it still happens.


int intX = 0
int intButtonPressed

while (0 == 0)
{
     intButtonPressed = GetRxInput(5)
     if (ButtonPressed > 192)
     {
          intX++;
     }
     if (intX > 7)
     {
          x == 0;
     }
     PrintToScreen ("%d\n",(int)intX);
}

…and the output on the window looks like this:
0
1
2
3
4
5
6
7
8
9
10…

Acording to my sense of logic, shouldn’t “intX” reset to 0? Maybe I need another programming class…

(p.s.- it’s the second “if” statement that’s ignored)

x == 0;

what, exactly, is this line supposed to be doing? I realize I’m getting ready to fall asleep and shouldn’t be looking at code but I swear it’s comparing x to zero, and I think you want to be setting intX to zero. (intX instead of x and = instead of ==). [or am I coding the wrong language in my head again?]

I don’t think it’s getting ignored. I just don’t see it doing anything.

I’m not sure where you’re going with this code, but again, I need to go to sleep. If you’re trying to toggle through eight settings, the code is going to fly through all those settings rapidly until you stop pressing the button, kind of like a roulette wheel. If you want a one button toggle, the code needs a process where it waits for you to stop pressing the button before allowing itself to increment the next time the button is pushed.


int intX = 0
int intButtonPressed

while (0 == 0)
{
     intButtonPressed = GetRxInput(5);
     if (ButtonPressed > 192)
     {
          intX++;
     }
     if (intX > 7)
     {
          intX = 0; // reset it back to zero <--THIS WAS THE BAD LINE
     }
     PrintToScreen ("%d\n",(int)intX);
  // PrintToScreen ("%d\n",intX); // Should not need to cast it, it's aready an int
}

Thanks for both of the replies, “==” needed to be “=”. Turns out that I do need another class…:rolleyes:

Sorry there-- I meant intX (x is an agebra thing), and now it seems to be working.

I already have the code for that… the == to = was the only thing that I was testing. My intention was to create an example code for here, as the toggle code is really long.

You should avoid using variable names like a,b,c,x,y,z because they have no meaning and make it very difficult to read the code. While it’s not a big deal on a small program like this it would be very hard on a program with 50+ variables and 300+ lines of code. :smiley:

Yeah, I know.:rolleyes: I just needed to test it, so I just used letters. The new Nelson III will have more variables, so I name them things like: intStickChan3, intFrontDist, and intEStop.

Much like the debate around writing comments, that’s a matter of opinion. For the above code, “intX” or even “x” is a perfectly readable name because the only purpose the variable serves is to be a counter.

Trying to name a variable simply because there’s a belief that says “named variables make reading code easier” can get you into just as much trouble as leaving them alone, but more importantly, it can cause the programmer to waste a lot of time performing a task that either doesn’t add value or doesn’t add as much value as some other task they could be doing.

The time to change “x” to a more meaningful name is when x has become important enough to need a more meaningful name. Until then, “x” works just fine. I’ve seen it work perfectly well in programs 300+ pages long, not just 300+ lines long.