|
December 25th, 2007
Because there is no bracketing, the code does not perform as you hope it would. Without brackets, the if statement only considers the next line to be inside the if. So in this case if (total==MAX) is true it will execute the statement if(total<sum), but if it is not it will not execute that statement. In either case it will print out the line after it which is print(total==Max...)
Without brackets it evaluates to:
if( total == MAX )
{
if( total < sum )
}
System.out.println("total==MAX...)
else
{
System.out.println("toatl is not ...);
}
That is why you always want to use brackets to make sure your code executes the way you want it to, it is also more readable.
|