Example code:
int hour = 0;
bool saveData = true;
if(hour > 0) doSomeMethod(); saveData = false;
In the code above, saveData will always be set to false, yet doSomeMethod() won’t be fired. I figured that the compiler treats the semi-colon after doSomeMethod() as an indicator to move to the next statement, ignoring that it’s on the same line as the if statement. What’s the reason for this behaviour?
An
ifstatement can contain either a single statement, or a code block. Once the compiler finds the;it ends theif.Your code above is equivalent to:
What you want is:
or: