View Single Post
(#3 (permalink))
Old
johnnyloot is Offline
Private
Points: 166, Level: 2 Points: 166, Level: 2 Points: 166, Level: 2
Activity: 0% Activity: 0% Activity: 0%
johnnyloot is an unknown quantity at this point
 
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Dec 2007
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.