Java If- else statements? - KingPin's Forum
KPsDenKPsDen ArmoryImage HostingLegendz text RPGIp DisplayIRCFlash Arcade
Forum
      |        
Register
         Mob levels... (Posted By:rangerd65 - Replies:0 - Views:6)      « »     WTS 3 KP's Campers (Posted By:pwnzors - Replies:0 - Views:7)      « »     1 time account reset (Posted By:Bundy - Replies:10 - Views:31)      « »     Halo 2 and all vista game compatible with XP fix (bp13) (Posted By:Headshot - Replies:1 - Views:68)      « »     So that's it!!? (Posted By:Headshot - Replies:9 - Views:73)      « »     annoying person (Posted By:SoloG - Replies:8 - Views:74)      « »     3 more weeks !!!! (Posted By:SoloG - Replies:5 - Views:76)      « »     lulz no acces in stv arena! (Posted By:Bionuclear - Replies:5 - Views:74)      « »     Wrath of the lich king (Posted By:Bionuclear - Replies:26 - Views:323)      « »     Missing Donation (Posted By:KingPin - Replies:1 - Views:10)      « »     
KingPin's Forum
 
K.P.s.N. Register vbBux / KPs Mall Bugs Blogs FAQ Search Today's Posts Mark Forums Read Donate
Go Back   KingPin's Forum > KP's Network Forum > RSS News
Reload this Page Java If- else statements?
 


RSS News This is a discussion on Java If- else statements? in the RSS News;
Description: what is wrong with the code fragment if (total == MAX) if (total < sum) System.out.println("total == MAX and is ...

Reply
 
LinkBack Thread Tools
Java If- else statements?
(#1 (permalink))
Old
puji_sonti is Offline
Private
Points: 178, Level: 2 Points: 178, Level: 2 Points: 178, Level: 2
Activity: 0% Activity: 0% Activity: 0%
puji_sonti is an unknown quantity at this point
 
puji_sonti
Rupees: 238.30
Bank: 500.00
Total Rupees: 738.30
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Dec 2007
Java If- else statements? - December 25th, 2007

what is wrong with the code fragment

if (total == MAX)
if (total < sum)
System.out.println("total == MAX and is < sum.");

else
System.out.println("total is not equal to MAX");

 
Reply With Quote
Revenue Shared Ads
(#2 (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
 
johnnyloot
Rupees: 2.20
Bank: 500.00
Total Rupees: 502.20
 
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.

 
Reply With Quote
(#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
 
johnnyloot
Rupees: 2.20
Bank: 500.00
Total Rupees: 502.20
 
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.
 
Reply With Quote
Revenue Shared Ads
Reply

Bookmarks

Tags
java, statements

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Rupees Per Thread View: 1.00
Rupees Per Thread: 15.00
Rupees Per Post: 5.00
Forum Jump



Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright 2004-2009 KPsN


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81