I was looking at my professor's notes and came across two things I didn't quite understand. Would someone either reword or work through them with me? I'm not quite sure what the two notes are saying.
"Missing" Braces. Some Java examples might seem to have missing braces on IF statements. Our textbook tries hard to use braces all the time (a very good idea!) but it turns out it does use them sometimes when they could be left off. Read the section on the IF statement in Java Programs for an explanation of "block" statements.
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.
Another IF Bug. Another common error is to use one equals sign where we meant to use two. In the following:
if ( count = 0)
{
count = count + 1;
}
the equal sign is not comparing count to zero, it is setting count to zero. The statement count = 0 itself evaluates to a value, namely the assigned value zero. So this technically isn't an illegal Java statement. It is the equivalent of:
if ( 0 )
{
count = count + 1;
}
Most compilers will assume you didn't mean to do this and will complain. But not all. Be very careful when using the == comparison.
Let the compiler(s) help us. Compile with warnings enabled, ideally with more than one compiler.
For things like unintended missing braces, use tool support to the extent possible;
use an editor that auto-indents or post-process with a source code indenter like astyle.
#include <iostream>
int main()
{
int a = 67 ;
// ********** typo: unintended semicolon **********************
// LLVM: warning: if statement has empty body
// GNU: warning: suggest braces around empty body in an 'if' statement
// Microsoft: warning ';': empty controlled statement found; is this the intent?
if( a > 0 ) ;
std::cout << "a is positive\n" ;
// ********** typo: = instead of == ********************
// LLVM: warning: using the result of an assignment as a condition without parentheses
// GNU: warning: suggest parentheses around assignment used as truth value
// Microsoft: warning: assignment within conditional expression
if( a = 67 )
std::cout << "a is equal to 67\n" ;
// *********** possible missing braces or bad indentation ****************************
// the GNU compiler shines here
if( a > 0 ) // GNU: warning: this 'if' clause does not guard...
++a ;
std::cout << "a is positive\n" ; // GNU ...this statement, but the latter
// is misleadingly indented as if it is guarded by the 'if'
}