• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in for loop

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"total > 30" implies "0 > 30"
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just check the condition in the for loop. It is false thats why the control did not get into the loop!
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I got it
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides, as far as I know, you CAN'T mix initializations AND declarations in the init part of the For loop.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Samuel Molina
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that makes sense...
Thanks. I'll study harder.
 
reply
    Bookmark Topic Watch Topic
  • New Topic