This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, Can any one explain me why in below code I am not anle to print anything what wrong with the for loop as far I see there is no error in the loop then why its neither even going intgo the loop nor printing total.
SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
The way I always think of "for loop" conditions (just because it helps me) think of the MIDDLE parameter as a while(this is true) thing!
Yours was while( total > 0 ) so, as long as total is greater than 30, the code will make a pass through the loop. Just above the loop, since you set total to 0 it starts OUT LESS THAN 30, so when you hit the for statement the first time, it is already false and you never take a pass in to the body of the for loop. You may have actually meant to do "total < 30" which would have the for loop going up from total = 0 to 29 which would be 30 trips through the body of the loop.
SCJP - 86% - June 11, 2009
Just a note to say that you can indeed define new variables in the init part of the for loop, so long as they are all of the same type. In other words, you can have ONE valid statement in the init part.
Note that the OP indicated that she was having an unexpected outcome which suggests a runtime issue (implying she got her code to compile).
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Joined: Jul 09, 2007
Wow, that makes sense... Thanks. I'll study harder.