Hello all, I am having a problem trying to nest several if statements within one another. It seems the condition of one of the ifs is being ignored and the code is being executed regardless. Here is the section of the code where I am having the problem. This section dims the backlite of my LCD until the screen is touched, then restores the backlite to full brightness but, it's supposed to wait 1 minute "if (dimmer_delay >= 60000);" before beginning the dimming again. The condition of "dimmer_delay" is ignored but the condition "(dimmer >= 1)" is operating as intended. Any help is greatly appreciated.
strikes again. This is probably the most common syntax error here. It's a shame the compiler can't give a warning when there's a semicolon immediately after the closing parenthesis of an if() statement, as it's pretty much guaranteed to be unintended, and if it was intended, it's still terrible style - yet also extremely easy to do on accident.
Thanks guys. I go blind to things after looking at them for so long. Easy mistake to make since the ";" is needed after most every line of code and if it is needed after every line then why is it needed at all? Shouldn't the compiler know what it's doing? Thanks again.!!! ;-)))
Ken_F:
Shouldn't the compiler know what it's doing?
The compiler knows exactly what it’s doing. It’s following the syntax of the Language Specification. The original code was in compliance with that specification.
A more correct question might be: “Shouldn’t the compiler issue a warning when I do stupid things like that?”. That might be nice. But, since I don’t write compilers, I don’t know how difficult implementing it would be.
Ken_F:
Thanks guys. I go blind to things after looking at them for so long. Easy mistake to make since the ";" is needed after most every line of code and if it is needed after every line then why is it needed at all? Shouldn't the compiler know what it's doing? Thanks again.!!! ;-)))
; marks the end of a statement. You can actually put more than one statement on a line. It is poor style (readability reasons), but it is legal, and there are some proponents of putting multiple related, trivial statements on one line. Put another way, the ; is what has the syntactical meaning at the end of the line. The newline is just for human readability.
So it stands to reason that a ; on it's own is an empty statement.
if(condition) executes the next statement or block only if condition is true. Many people will always make a block after an if, even if only executing one statement, for readability reasons - but the language doesn't require it.
And thus we arrive at the situation we have now....
gfvalvo:
A more correct question might be: “Shouldn’t the compiler issue a warning when I do stupid things like that?”. That might be nice. But, since I don’t write compilers, I don’t know how difficult implementing it would be.
Why should the compiler issue a warning about something that is perfectly valid, legal syntax? Should it then also issue a warning on "while (1);" ?
The onus is entirely on the programmer to learn the language syntax, as it should be.
Regards,
Ray L.
Many people will always make a block after an if, even if only executing one statement, for readability reasons - but the language doesn't require it.
In my opinion the use of { and } should be mandatory after if, else, while, do, etc but I am sure that my opinion is unlikely to cause the language definition to be changed !
RayLivingston:
Why should the compiler issue a warning about something that is perfectly valid, legal syntax? Should it then also issue a warning on "while (1);" ?
The onus is entirely on the programmer to learn the language syntax, as it should be.
Regards,
Ray L.
The compiler emits warnings for similar common mistakes, even if they're legal. For example, "if (x = y)" will cause GCC to warn about using = instead of == (and it says how to make it clear that it's intentional to suppress the warning).