Page 1 of 1

About Question com.enthuware.jfcja.v8.2.380 :

Posted: Wed Apr 12, 2023 3:56 am
by Maria Teresa Lorenzo
Hi,

Can anybody explain the workings of the second continue statement below? my understanding is that i == 3, will skip the index position of 4 and move to the next number, 5. - Thank you for your help.

What can be inserted in the following code so that it will print exactly 2345 when compiled and run?

Code: Select all

public class FlowTest {

    static int[] data = {1, 2, 3, 4, 5};

    public static void main(String[] args) {
        for (int i : data) {
            if (i < 2) {
                //insert code1 here
            }
            System.out.print(i);
            if (i == 3) {
                //insert code2 here
            }
        }
    }
}

Re: About Question com.enthuware.jfcja.v8.2.380 :

Posted: Wed Apr 12, 2023 1:40 pm
by admin
No, a continue statement doesn't skip the whole iteration. It only skips the remaining part of the current iteration. So, if you put continue inside if (i == 3) block, it will skip the remaining statements of that block for this iteration. But there are no statements after that anyway, so it is inconsequential. The loop will perform the next iteration with i = 4.