1

So I'm working on Java Koans and I'm stuck on number 69. Here's the code:

@Koan
public void forLoopContinueLabel() {
    int count = 0;
    outerLabel:
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 6; j++) {
            count++;
            if (count > 2) {
                continue outerLabel;
            }
        }
        count += 10;
    }
    // What does continue with a label mean?
    // What gets executed? Where does the program flow continue?
    assertEquals(count, __);
}

assertEquals checks if the answer is correct - it sends Koans both arguments and if they match you advance. For example, if one wrote assertEquals(3 + 3, 6) it would be correct.

The double-underscores mean REPLACE ME. In the Koans application it says that I need to replace the underscores with 8, but I don't understand exactly how the continue outerLabel works.

So my question is: Why is count 8?

Thanks in advance. Any help would be appreciated.

MD XF
  • 7,860
  • 7
  • 40
  • 71
  • There's a bit about it in the [official tutorial](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html): *A labeled continue statement skips the current iteration of an outer loop marked with the given label.* – UnholySheep Oct 14 '16 at 21:18
  • What do you expect? – Yev Oct 14 '16 at 21:20
  • 2
    And to for a bit more detailed explanation: Once your count variable becomes bigger than 2 (which happens when `i` is 0 and `j` is 2) the line `continue outerLabel` will always skip out to the beginning of the loop (skipping `count += 10`) and then iterating until `i` becomes 6 (counting up the iterations you will see `count` ends up at the value 8) – UnholySheep Oct 14 '16 at 21:23
  • Possible duplicate of [What is the "continue" keyword and how does it work in Java?](http://stackoverflow.com/questions/389741/what-is-the-continue-keyword-and-how-does-it-work-in-java) – Polygnome Oct 14 '16 at 21:25
  • 2
    *"why did this question merit a downvote"* -- Because you *could* have researched the well-documented features of "`continue` with a label" – OneCricketeer Oct 14 '16 at 21:27
  • Put a `print` statement after `count++` that prints `i`, `j`, and `count`. *Observe what happens.* This is called **debugging**, and will answer your question of why `count` is what it is. Unless your question is "what does `continue outerLabel` do?", but that's something a Java manual can tell you. You might also want to read "[What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149)", where the "problem" is your lack of understanding. – Andreas Oct 14 '16 at 21:47

2 Answers2

1

continue outerLabel; force to skip the second for.

Although the second for intends to iterate 6 times, it actually iterate only 3 times when i==0 and once for i>0.

Paulo
  • 2,956
  • 3
  • 20
  • 30
1
  • Only for i is 0 the j is 0, 1, 2.
  • For the remaining 5 i's only j is 0
  • 1*3 + 5*1 = 8

Or

i   j  count
=   =  =====
0   0  0     count++
       1     count++
    1  2     count++
    2  3     count++; continue outerLabel
1   0  4     count++; continue outerLabel
:   :  :     :
5   0  8     count++; continue outerLabel
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    the short answer would be: count increments by 3 for the first turn, and by a total of 5 for the remaining five turns of the outer loop. – Alexander Stohr Aug 24 '21 at 14:59