• 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

For loop

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is straight from the Kathy Sierra book about for loop
Keep in mind that this iteration expression is always the last thing that happens ! So although the body may never execute again, the iteration expression always runs at the end of the loop block. so for a for loop like this does it mean that when i is equal to 5 the body does not execute but still i is incremented ? but the output is 5. So is this a printing mistake or there is something else that the author wants to bring attention to.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pujari,




does it mean that when i is equal to 5 the body does not execute but still i is incremented ? but the output is 5. So is this a printing mistake or there is something else that the author wants to bring attention to.

It never means that "when i is equal to 5 the body does not execute but still i is incremented"

1- Declaration and Inilialization/Initialization takes place once.
2- Condition is checked/ if false exit from the loop
3- Body of the loop is executed
4- Increment/decrement or say the third part of the loop executes, there can be print statement like things too.

Goto step 2


Thanks and Regards,
cmbhatt
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rohan,

Adding to Chandra,

Consider small example

boolean volatile No_Reply[] = {true , true, true, true};
String[] bartenders = { "Bert Bates", "Henry Wong", "Barry Gaunt","Marc Weber" };

for (byte i=0; i < bartenders.length ; i++){

synchronize(bartenders[i]){
if(No_Reply[i]){
S.o.p("I am longing for you: "+ bartenders[i]);
bartenders[i].wait(10000);
if ( i == bartenders.length -1) i = -1;
// Concentrate Here
// Here you need to take care you want i be inited with 0
// but here you can't put i=0; because next i++ executes,
// thus i becomes 1 and 1 < bartenders.length evaluates.

}
}

Thats the intent of warning given in your post.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roohan Pujari:
This is straight from the Kathy Sierra book about for loop
Keep in mind that this iteration expression is always the last thing that happens ! So although the body may never execute again, the iteration expression always runs at the end of the loop block. so for a for loop like this does it mean that when i is equal to 5 the body does not execute but still i is incremented ? but the output is 5. So is this a printing mistake or there is something else that the author wants to bring attention to.



It helps to understand what's happening if you rewrite the for loop as a while loop.



is equivalent to

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please check the programe again...It is something like this
1. int i=0;
2. for(i=0;i<5;i++)
3. {
4. System.out.println(i);
5. }
System.out.println(i);

SO here when the for loop will exit , the value of i wud be "5" bcoz when the value of i was 4 thn the conditional statement was true and so i got incrimented by 1.

So kindly check the question again
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic