Decrement Operator Not Working

Hi, I'm super new to arduino. I've written some simple code to test out the output for the decrement operator through the serial monitor.

For some reason it doesn't work and shows the exact same value. Any tips would be helpful.

[code]
int v = 6;
void setup() {
  Serial.begin(9600);
  Serial.println(v--);

}

void loop() {

}
[/code]

v-- mean post decrement or decrement after the current value is used

try pre-decrement --v

Works well in my case!


int v = 6;
void setup() 
{
  Serial.begin(9600);
  Serial.println(v--);
  Serial.println(v--);
}

void loop() {}

Output:

6
5

Put that code in a loop, or put it in your loop() function.

You might want to add a delay(777) in the loop, however you loop it, to,slow down the printed results.

a7

If you open an new sketch you see this

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

void setup()
// put your setup code here, to run once:

void loop()
// put your main code here, to run repeatedly:

best regards Stefan

1 Like

Missing the closing braces (}) for both functions.

removed the opening curly braces as well. The important message is the red words

For whom?

666 works even better.

1 Like

Oh my, he also removed the “//“ to indicate a comment. The question was answered in the first response, so now let’s get out our micrometers and compare our various programming skills.

Stop hijacking threads to massage your egos.

Sorry, I'm curious for whom this particular announcement is necessary?

My dad had a saying: “if you throw a rock into a pack of dogs, the one that yelps is the one you hit”. Perhaps you can break that down to get your answer.

I will not respond to further questions about this. I just get exasperated by the type of behavior seen here repeated over and over.

¯_(ツ)_/¯

Throw a rock too hear a dog yelp...

OP was expecting 5 at this point, so post #2 gets the prize.

5 is here. You have not printed my whole output.


int v = 6;
void setup() 
{
  Serial.begin(9600);
  Serial.println(v--);
  Serial.println(v--);
}

void loop() {}
Output:

6
5

You have succeeded in demonstrating the post decrement operator behavior very well.

2 Likes

Your code CAN get to 5, but OP was looking for 5 on the first iteration, not the second. See post #2.

At least we can breathe a bit of relief - Decrement Operator still works!

a7

3 Likes

Woosh.