• 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

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

[JAM -- edited to include the [CODE] and [/CODE] tags]

Output will be 3 4 5.


Help is appreciated ,how does this code work.I am not able to figure out .
[ December 21, 2004: Message edited by: Joel McNary ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's time to play computer! Get a fresh sheet of paper and make a column for i and a column for j.

i starts at 0. While i is zero, we loop j from 5 down to 3.

No matches, no display. Next i is 1 and we loop j again. No matches. Let's see when i is 3

I'm not sure what part of the problem was not clear before. We usually see for loops going up, not down, so maybe that was unexpected. Did that help?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps what was confusing what the use of the label. In Java, you can label a loop. When you continue (or break) that label, it continues (or breaks) the appropriate loop. This is a handy little shortcut -- however, it's not widely used because it makes the code much more difficult to read.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi perna
this is the solution for ur question
step 1: i=0;j=0
step 2: i=0;j=1
...
goes on till i=3 but all the time it does not enter the if loop since j`s count stops at 3

step 3: i=3;j=5
.
.
step 4:i=3;j=3 now it prints the value 3 and goes to lab label and continues with i=4
similarly 4 n 5 r printed
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandy,

I'm not agree with you, your code ::

code :
-----------------------

step 1: i=0;j=0
step 2: i=0;j=1
.....

-----------------------

where value of j you are assuming wont start with 0, it starts with 5 go on decreasing the value till j > 2. so whatever Stan James has explained is very helpful & the correct also.

And about the label :: if you dont use the label then also the output is the same or i can say it wont affect the program.

So my Qn is to prerna boja that why are you using the label? i also dont understand the reason of using Label.

Thanxs
Sheetal
[ December 22, 2004: Message edited by: Sheetal Kaul ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't have a label, and "continue" or "break" statement effects the INNER most loop you are in.

it looks like in THIS example, the end output would be the same if you didn't use a label. however, it will take longer (not so much longer that you would notice).

when we get to i==4 and j==4, we print out 4. as the code is written, we then continue the OUTER loop. we increment i to 5, reset j to 5, and proceed. we print 5, continue the outer loop, which then fails it's test, so it exits.

if you take out the label, we get to the print 4. you continue, but without the label, you continue on the inner loop. so j becomes 3, we test and fail on i==j. we now increment i to 5. again, after we print the 5, we would continue on the inner loop, testing 5==4 and then 5==3... etc.

now, you would probably not notice this slowdown in performance for such small ranges, but if i was going from 1 to 1,000,000,000 and j from 100,000 to 1, you probably would see the slowdown.
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Now i understood the situation here, why he label is used, thanxs fred rosenberger for the explaination.


Thanxs
Sheetal
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic