Skip to content
Advertisement

How to skip 2 iteration in java loop

I have a loop where single iteration is skipped using continue:

for(int i=0;i<5;i++){
            if(i==2){
                continue;
            }
            System.out.println(i);
        }

Output would be 0 1 3 4

Based on my criteria above like i==2, I want to get output 0 1 4. Meaning I want to skip 2 iterations. How do I do that?

Advertisement

Answer

for(int i=0;i<5;i++){
    if(i==2){
        i++
        continue;
    }
    System.out.println(i);
}

Increment i by one inside the if statement.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement