Since nearly 40 years I am programming in "C", later in "C++", and since a lot of years I do not need references for the language any more.
But yesterday I detected a mistake in my program, which I did not expect. I doubt, whether it is a syntax error I make, or the compiler (arduinoPlugin/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-g++) is not working correct.
It is a very simple routine:
if(i > 0)
a = true;
else
b = true;
As expected, setting i to 0 before makes b true. The same behavior (as expected) with
if(i > 0)
{
a = true;
}
else
{
b = true;
}
But mixing brackets and non brackets with
if(i > 0)
{
a = true;
}
else
b = true;
is not setting b to true.
(This routine is contained in a library/class and b is public and tested with another program.)
And this I do not understand.
Are my thousands of programmed C code lines before simply without such an error and did I make this error the first time in my life?
Or is it an error of the compiler, because my understanding of using brackets in such routines is: You need brackets to aggregate multiple lines/statements handled like one statement. If there is only one statement/line, there is no need for brackets.
Does someone know the circumstances of using brackets in this case and can give me a hint?
bool a, b;
void setup() {
Serial.begin(115200);
for (int i = 0; i < 2; i++) {
a = false;
b = false;
if (i > 0) {
a = true;
} else
b = true;
Serial.print(F("i = "));
Serial.print(i);
Serial.print(F(", a = "));
Serial.print(a);
Serial.print(F(", b = "));
Serial.println(b);
}
}
void loop() {}
Hello
I can´t replay your issue by using this test sketch:
int i=0;
if (i > 0)
{
Serial.println("a=true");
}
else
Serial.println("b=true");
i = 1;
if (i > 0)
{
Serial.println("a=true");
}
else
Serial.println("b=true");
Thank you very much (both).
Meanwhile I tested the routine in different levels and it is as You found out, that there is no need for the extra brackets.
My routine with the error is located very deep in a switch-statement inside a public method of a class.
Because I have no errors when I implement the extra brackets, I think the problem is with the compiler at this level of coding.
The combination of switch-case and brackets can do that, but only if you put a bug in it
The switch-case can not have local variables in a 'case', but it can if brackets are used. So if you declare a local variable with the same name as a already existing variable then the behavior of the compiler might change with brackets.
If you use (bad) macros, they might cause this as well.
@profrob, please show us a sketch that shows the problem. Tell which Arduino IDE is used and which Arduino board. If you can not prove your claim, then it does not exist
I wanted to be sure, that the syntax in if/else with mixed brackets and non brackets is OK. And the answers are very helpful and confirm the correctness.
But it may be, that the error comes from a wrong usage in the context.
Here is the routine, where the error occurs:
Do not think about the creating of the average of measurements, it is the first draft of the routine and not tested. Till now I only tested with avgSetAG = 0 and detected that problem with or without the brackets (marked with asterisks).
I do not use local variables of the run-function, all variables are declared with the class.
Without the brackets I do not get values because the application is waiting for newValueAG == true. With the brackets it works as expected.
I mentioned the compiler at the beginning of the thread.
My IDE is Eclipse/Sloeber and the Board is Arduino Nano BLE 33.
Thank You for Your interest and help.
The sketch (application) itself is rather big and using more libraries. It shows the measurement values formatted on a serial terminal to check the moving of the Arduino board.
For those of You, who thought about the averaging in that routine:
Meanwhile it is tested (OK), I added the reset (=0) of the sum values in the listing above. The initialization of averaging is done by avgSetAG = avgCntAG = number of values.
For those of You, who did not recognize the two different if-levels, I added two comment lines //---- after the coupling brackets.
The application synchronizes to newValueAG == true. So with number 10 in avgSetAG, the output to the terminal is simply 10 times slower.
Finally I found the error.
It is my mistake.
For controlling the TWI communication to fetch sensor data I used the same variable as for observing sensor status. But that is wrong, because I hand over different variables for the two TWI procedures.
What happened is, that the discussed if-sequence (data fetch state) was only reached randomly.
That this was synchronous to my changes of using brackets or not with recompiling and testing, is another random matter.
It is like a series of changing red and black with roulette.
After repairing the TWI check, everything works fine and I can use the mixture of brackets and non brackets as we all expected.
And of course the compiler is working correct.
So I thank you all again for the discussion which helped me to continue searching the error.