If Bug

I don't quite understand what my professor is saying here and I was hoping someone would help me break it down. I know this isn't Java, but still, I figured you guys would be able to decipher what's going on.


The "Nasty" IF Bug. A quirk of Java is that it considers "empty" statements to be legal. There is nothing wrong with:

A = B * 100;
;
System.out.println("The value of A is " + A);

That semicolon sitting by itself is a do-nothing statement. BUT...that means that this IF statement is incorrect:

if (A > 0) ;
{
System.out.println("The value of A is " + A);
}

The semicolon after the parentheses makes this behave like:

if (A > 0)
;
{
System.out.println("The value of A is " + A);
}

which says in English, if A is greater then zero, then do nothing at all. The println statement is always executed as the next statement in sequence. The compiler will not complain about the extra semicolon. Even though it makes no sense, it is technically legal.
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/193325/
Sorry for the repeat. I was unaware I made another thread.
Topic archived. No new replies allowed.