Java continue statement

Continue:

Continue statement use with loop (for loop, while loop, do-while loop)

It is used for the skip some result or statement while execution of the loop

Syntax:

continue;

basic example :

class Cont {
    public static void main(String[] args) {
      
        // for loop
        for (int i = 1; i <= 10; ++i) {

            // if value of i is between 6 and 9, continue is executed 
            if (i > 6 && i < 9) {
                continue;
            }      
            System.out.println(i);
        }   
    }
}
1
2
3
4
5
6
9
10

 Explanation of the continue statement:

Suppose you have to  separate fresh mango 🥭 in the bucket (mix mango rotten and fresh) and put into a new bucket

It’s simple  so you skip rotten mango and take fresh mango from the bucket

so continue statement do same things

continue statement skips some statement on the specific condition

let us take a logical example to understand better

we have to write a program which prints all odd number between 1 to 50

but we don’t want odd no 15 to 30

class JavaExample {
   public static void main(String args[]) {
	int n = 100;
	System.out.print("Odd Numbers from 1 to "+n+" are: ");
	for (int i = 1; i <= n; i++) {
	   if (i % 2 != 0) {

		if (i > 15 && i < 30) 
		{
                   continue;
		   
	        }

		
		System.out.print(i + " ");
	   }
	}
   }
Odd Numbers from 1 to 50 are: 1 3 5 7 9 11 13 15 31 33 35 37 39 41 43 45 47 49 


SHARE THIS PAGE





Important Link