# confusing for Loop.

sonir shah

Ranch Hand

Posts: 435

Roy Ben Ami

Ranch Hand

Posts: 732

DC Dalton

Ranch Hand

Posts: 287

posted 14 years ago

If you put println's inside each for loop & one right after c is incremented you will see this result:

F:\JavaProgs>java Test

i is: 0

j is: 0

k is: 0

c is: 1

k is: 1

c is: 2

j is: 1

k is: 0

c is: 3

k is: 1

c is: 4

k is: 2

c is: 5

i is: 1

j is: 0

k is: 0

c is: 6

k is: 1

c is: 7

j is: 1

k is: 0

c is: 8

k is: 1

c is: 9

k is: 2

c is: 10

10

If you use this to follow the loops thru I think you will get it.....you have to remember that when you break out of a loop to another loop the inner loop variable will be re-initialized when it is hit again. Look at the code & these results & try writing it down in a small chart.....I thikn this will help you see whats up.

[ January 13, 2002: Message edited by: DC Dalton ]

F:\JavaProgs>java Test

i is: 0

j is: 0

k is: 0

c is: 1

k is: 1

c is: 2

j is: 1

k is: 0

c is: 3

k is: 1

c is: 4

k is: 2

c is: 5

i is: 1

j is: 0

k is: 0

c is: 6

k is: 1

c is: 7

j is: 1

k is: 0

c is: 8

k is: 1

c is: 9

k is: 2

c is: 10

10

If you use this to follow the loops thru I think you will get it.....you have to remember that when you break out of a loop to another loop the inner loop variable will be re-initialized when it is hit again. Look at the code & these results & try writing it down in a small chart.....I thikn this will help you see whats up.

[ January 13, 2002: Message edited by: DC Dalton ]