LogIn
I don't have account.

Continue Statement in Java

DevSniper
197 Views

In development , efficiency and control over loops are essential for writing optimized, readable robust and error free code. continue is a such type of powerful control flow statement that allows developers to control the flow within loops.

In programming , continue statement is used to skip the current iteration of the loop and proceed to the next iteration of loop immediately. It is useful where you want to skip loop logic, based on certain condition in loop without exiting loop. You can use java continue statement with all type of loop such as for , while and do while loop.

  • continue statement is used to skip the current iteration of the loop and proceed to the next iteration of loop immediately.
  • In case of nested loop it start new flow only the innermost loop that contains it.
  • continue enhances code readability by clearly defining skip points of loop.
  • continue statements is useful where you need bypass certain iterations entirely based on business logic or specific requirements.
  • Continue statement with for loop
  • Continue statement with while loop
  • Continue statement with do while loop
  • Continue statement with nested loop

Continue statement with loop

You can use continue statement with loop where you want to skip loop logic, based on certain condition in loop without exiting loop. You can use java continue statement with all type of loop such as for loop, while loop and do while loop.

Continue statement with for loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        for(int i=1; i<4; i++) {
            System.out.println("brfoer continue i : "+i);
            if(i==2) {
                System.out.println("CONTINUE FOR LOOP for i : "+i);
                continue;
            }
            System.out.println("after continue i : "+i);
        }
    }
}
brfoer continue i : 1
after continue i : 1
brfoer continue i : 2
CONTINUE FOR LOOP for i : 2
brfoer continue i : 3
after continue i : 3

Continue statement with while loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int i=1;
        while(i<4) {
            System.out.println("brfoer continue i : "+i);
            if(i==2) {
                System.out.println("CONTINUE while LOOP for i : "+i);
                i++;
                continue;
            }
            System.out.println("after continue i : "+i);
            i++;
        }
    }
}
brfoer continue i : 1
after continue i : 1
brfoer continue i : 2
CONTINUE while LOOP for i : 2
brfoer continue i : 3
after continue i : 3

if we forget to mention increment/decrement along with continue statement this while loop will become infinite loop . for example if we change single line in above example, by removing i++ from if condition loop will converted into infinite loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int i=1;
        while(i<4) {
            System.out.println("brfoer continue i : "+i);
            if(i==2) {
                System.out.println("CONTINUE while LOOP for i : "+i);
                continue;
            }
            System.out.println("after continue i : "+i);
            i++;
        }
    }
}
brfoer continue i : 1
after continue i : 1
brfoer continue i : 2
CONTINUE while LOOP for i : 2
brfoer continue i : 2
CONTINUE while LOOP for i : 2
brfoer continue i : 2
CONTINUE while LOOP for i : 2
............................................................
.............................................................

Continue statement with do while loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int i=1;
        do{
            System.out.println("brfoer continue i : "+i);
            if(i==2) {
                System.out.println("CONTINUE do while LOOP for i : "+i);
                i++;
                continue;
            }
            System.out.println("after continue i : "+i);
            i++;
        }while(i<4);
    }
}
brfoer continue i : 1
after continue i : 1
brfoer continue i : 2
CONTINUE do while LOOP for i : 2
brfoer continue i : 3
after continue i : 3

if we forget to mention increment/decrement along with continue statement this do while loop will become infinite loop . for example if we change single line in above example, by removing i++ from if condition loop will converted into infinite loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int i=1;
        do{
            System.out.println("brfoer continue i : "+i);
            if(i==2) {
                System.out.println("CONTINUE do while LOOP for i : "+i);
                continue;
            }
            System.out.println("after continue i : "+i);
            i++;
        }while(i<4);
    }
}
brfoer continue i : 1
after continue i : 1
brfoer continue i : 2
CONTINUE do while LOOP for i : 2
brfoer continue i : 2
CONTINUE do while LOOP for i : 2
brfoer continue i : 2
CONTINUE do while LOOP for i : 2
.......................................................
.......................................................

Continue statement with nested loop

if we place one loop inside another loop this is called nested loop. inner loop execute completely for every outer loop execution. we can place continue statement inside or outside loop. if we place continue statement inside inner loop that will skip only inner loop current iteration without any impact on outer loop and if we place continue statement inside outer loop and outside of inner loop this will skip outer loop current iteration.

Continue statement inside inner loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        for(int i=1; i<4; i++) {
            System.out.println("outer loop i : "+i);
            for(int j=1; j<4; j++) {
                if(j==2) {
                    System.out.println("CONTINUE INNER LOOP FOR j : "+j);
                    continue;
                }
                System.out.println("\tinner loop j : "+j);
            }
            System.out.println("outer loop after inner loop i : "+i);
        }
    }
}
outer loop i : 1
        inner loop j : 1
CONTINUE INNER LOOP FOR j : 2
	inner loop j : 3
outer loop after inner loop i : 1
outer loop i : 2
	inner loop j : 1
CONTINUE INNER LOOP FOR j : 2
	inner loop j : 3
outer loop after inner loop i : 2
outer loop i : 3
        inner loop j : 1
CONTINUE INNER LOOP FOR j : 2
	inner loop j : 3
outer loop after inner loop i : 3

Continue statement inside outer loop and outside inner loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        for(int i=1; i<4; i++) {
            if(i==2) {
                System.out.println("CONTINUE OUTER LOOP FOR i : "+i);
                continue;
            }
            System.out.println("outer loop i : "+i);
            for(int j=1; j<4; j++) {
                System.out.println("\tinner loop j : "+j);
            }
            System.out.println("outer loop after inner loop i : "+i);
        }
    }
}
outer loop i : 1
        inner loop j : 1
	inner loop j : 2
	inner loop j : 3
outer loop after inner loop i : 1
CONTINUE OUTER LOOP FOR i : 2
outer loop i : 3
        inner loop j : 1
	inner loop j : 2
        inner loop j : 3
outer loop after inner loop i : 3