Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

finding values in two dimensional loop  RSS feed

 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm reading the OCA book and there is an example that I'm not really understanding. I run the code myself, it works but I still have trouble understanding the whole logic. So, here is the code:
package loops;


So, let me explain. The commented out code

finds the first matching and prints out the result: 1,1. OK that's clear.
The second matching snippet instead:

prints the second matching 2,0. Here I would have expected it to print the last matching instead, 2,2. So what I don't get is why it prints the second matching and comes out of both loops. Here the break statement should allow the application to come out of the inner loop only not the outer. What is it that makes it break from the outer loop as well?

Third snippet:

I think this is clear,  no break statement the loop continue executing. ANy idea?
thanks
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
The second matching snippet instead:

prints the second matching 2,0. Here I would have expected it to print the last matching instead, 2,2. So what I don't get is why it prints the second matching and comes out of both loops. Here the break statement should allow the application to come out of the inner loop only not the outer. What is it that makes it break from the outer loop as well?


Basically, the code finds the first one... just like in the first snippet. However, since the break statement doesn't break out of both loops (it just breaks out of the inner one), the code continues (with the next outer loop element) and finds the second matching case. And again, the break statement doesn't break out of both loops (it just breaks out of the inner one). However, in this second case, the outer loop is already at the last element. So, the outer loops merely completes, and the code after the loop reports that it found the second matching case.

Henry
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, thanks. Just so I understand it correctly though...when the value 2 in position 2,0 is found and the break executes, control goes back to the outer loop but the value of i is still 2 and i<list.length is true. Does i++ gets executed as soon as control comes back to the outer loop? that's the only way I can explain i<list.length being false. Hope that makes sense
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:when the value 2 in position 2,0 is found and the break executes, control goes back to the outer loop but the value of i is still 2 and i<list.length is true. Does i++ gets executed as soon as control comes back to the outer loop? that's the only way I can explain i<list.length being false. Hope that makes sense


It doesn't matter whether the "i<list.length" expression is true or false, upon completing the inner loop. Expressions are only checked at the top of the loop, and only after initialization (i=0) or reinitialization (i++) has executed. So, the inner loop completes, then the code runs to complete the current iteration of the outer loop. Upon that, the reinitialization code is executed, and then the expression is checked, to see if another iteration should run.

Henry
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks, I think I understand now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!