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]
gcjr
August 3, 2023, 4:53pm
2
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
StefanL38:
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
Missing the closing braces (}) for both functions.
removed the opening curly braces as well. The important message is the red words
paulpaulson:
For whom?
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...
xfpd
August 5, 2023, 5:25am
15
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
xfpd
August 5, 2023, 12:26pm
18
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