aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in for loop " Watch "Doubt in for loop " New topic
Author

Doubt in for loop

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
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)
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1558
"total > 30" implies "0 > 30"


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Its starightforward isn't it
int total=0;
for (int i = 0, j = 10; total > 30; ++i, --j)
0>30 false and hence it does not enter the for loop and result is 0
Thanks
Deepak


SCJP, SCWCD, SCBCD
Polishetty Raghavendra
Greenhorn

Joined: Apr 09, 2006
Posts: 18
Here the Value of total never more than 0 so it's printing the value as '0'.

If the Block of statements excuted then the value for total will change.

Regards
Raghavendra


Regards,<br />P Raghavendra<br />SCJP 1.5 - 70%
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Just check the condition in the for loop. It is false thats why the control did not get into the loop!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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.


------------------------
Bob
SCJP - 86% - June 11, 2009
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Thanks I got it
Samuel Molina
Greenhorn

Joined: Jul 09, 2007
Posts: 2
Besides, as far as I know, you CAN'T mix initializations AND declarations in the init part of the For loop.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 258

Originally posted by Samuel Molina:
Besides, as far as I know, you CAN'T mix initializations AND declarations in the init part of the For loop.


Welcome to Java Ranch, Samuel.

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).

- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Samuel Molina
Greenhorn

Joined: Jul 09, 2007
Posts: 2
Wow, that makes sense...
Thanks. I'll study harder.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in for loop