3

This is one nasty bug that took me half an hour to resolve. The compiler believes this is valid code and when it evaluates the statement, it will still call foo():

if(true && false);{ foo(); }

Why is it valid and why does foo() still get called?

6
  • 4
    Doesn't it do that because you've got a semi-colo after the if statement?
    – Iain Ward
    May 13, 2011 at 10:59
  • 1
    Fix this and all of your future problems by configuring VS to treat all warnings as errors. Check your project's Properties to do that. May 13, 2011 at 11:03
  • Flagged as not a real question. I would also have flagged it as a fallacy but that wasn't an option. ;)
    – J. Steen
    May 13, 2011 at 11:11
  • 2
    @J. Steen: I turned it into a question. At least, I hope I did.
    – BoltClock
    May 13, 2011 at 11:14
  • @BoltClock, while I doubt the intent of the OP was to ask a question, I approve of the edit. =) Unfortunately, one cannot unflag.
    – J. Steen
    May 13, 2011 at 12:04

8 Answers 8

22

Well, you didn't ask a question, but I feel obliged to tell you why the compiler is right and the code is valid (sorry, folks of SO):

if (true && false);

Is just an empty statement that executes if the condition is met (in which case it's not being met because of the && false, so nothing happens (but either way nothing will happen)). The statement is empty because the closing parenthesis is immediately followed by the semicolon.

{ foo(); }

Is a method call enclosed in a scope block. The semicolon terminates the if condition, so this block is not relevant to the if condition at all. It's just a block, by itself. The code is executed regardless of the if condition.

1
  • 6
    In summary: the problem is the semicolon (;) after the if statement, not the fact that you've combined true and false. May 13, 2011 at 11:00
8

It is valid code. It is an if with an empty statement, and a scope block.

In fact my compiler gives a warning for the if: Possible mistaken empty statement

If you had written:

if (true && false) { foo(); }

it would have the effect you expect.

But, if the real question is why it took you so long to find the problem, then I can tell you the problem is that you are not reading the warnings the compiler generates.

1
  • I can tell you the problem is that you are not reading the warnings the compiler generates - or maybe there's 291 warnings to read... Feb 3, 2014 at 8:35
5

This is valid code and not a bug. Note the additional ; you have between the if and the braces.

What your code amounts to is

if (true && false);

//...

{ foo(); }
2

remove the semicolon between ) and {

2
if (true && false) {

foo();

}

I think this is what you want.

If you just do

if(true && false);

it will stop your if-statement at the ; so all the code underneath:

foo();

is executed.

2
  • I mean, I suppose that's what the asker wants. I'm not really sure why you'd ever want to write if (true && false)... May 13, 2011 at 11:02
  • 1
    Maybe he's just writing true and false to make it simple for us :p There might be a whole statement in his code.
    – Matthias
    May 13, 2011 at 11:03
1
if(true && false);

;( semicolon) breaks the if loop and goes to foo();

try

 if(true && false)
    { foo(); }
1

You can claim that the specification is stupid, but you can't claim that it's a bug in the compiler. I would have made it illegal to put a ; directly behing an if, since I see no case where that's useful.

And since you even get a compiler warning this shouldn't be that big a problem in practice.

-1

The correct answer has already been given, and this is not an additional answer, but it's worth pointing out anyway: Use jslint and you could have avoided this problem.

For example, here's a Javascript buffer in emacs, with flymake-for-javascript - emacs' on-the-fly syntax checker - enabled:

enter image description here

(ps: that image also uses flymake-cursor.el, to automatically display the error message down below)

JSLint can be run from the command line, can be included in an automated build process, or it can be integrated into a development tool. If you haven't checked it out, do so. You'll learn some good habits for Javascript coding.

1
  • 2
    Erm, what the hell does Java or Javascript have to do with a question about C#? May 14, 2011 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.