Forums Register Login

For Loop

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi,

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


Thanxs
Sheetal
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 569 times.
Similar Threads
Some problem
arrays
doubt on continue loop
for loop question
LOOPS
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:29:14.