What do you mean it doesn't work right? This won't even compile.
Please read the sticky thread at the top of the board. Specifically the one titled "How to Use This Forum" before posting anything else. Use code tags when posting code.
What do you mean it doesn't work right? This won't even compile.
Please read the sticky thread at the top of the board. Specifically the one titled "How to Use This Forum" before posting anything else. Use code tags when posting code.
It does compile, it is another way to write it, even if it's not the traditional....
For what values of i, in the range 0 to 0, will i be greater than i+1?
It's actually post-increment, so it might even be undefined! I don't feel like deciphering the sequencing rules right now to try and figure out what it'll actually do. It might check if i is greater than itself, or if i+1 is greater than itself.
Either way, this is the kind of code that makes you think "What do you even want this to do?".
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("starting");
int i=1;
Serial.print( "1. (i > i) ");
Serial.println( (i > i) );
Serial.print( "2. (i > i++) ");
Serial.println( (i > i++) );
Serial.print("i = ");
Serial.println(i);
}
void loop() {
// put your main code here, to run repeatedly:
}
The results of 1. and 2. are 0 and 1, respectively.
So, the Arduino, aided and abetted by gcc, seems to think that "i" is greater than "i++" !
Those that use side effects in expressions need to understand the rules! "i > i++" is undefined.
I don't think that Brian was trying to use side effects to some advantage. He is a newbie. I don't want to know what went thru his mind when he came up with this construct. But he already knows that his code does not work and asked for help.