Java break and continue

Java break

Java break is keyword which is used to going out from “switch” and loops(while, do-while, for).

class SampleBreak
{
   public static void main(String args[])
   {
        int num= 1;
	while (num <= 10) {
	  System.out.println(num);
          if(num==5)
          {
              break;
          }  
	  num++;
	}
   }
}

When num will be equal to 5 then it will jump out from the current loop.

Output:

1
2
3
4
5

 

Java continue

Java continue is also a keyword which is used to going to next condition in loops(while, do-while, for) and going to next case in switch.

class SampleContinue
{
   public static void main(String args[])
   {
        int num= 1;
	while (num <= 10) {
	  
          if(num==5)
          {
              continue;
          }
          System.out.println(num);  
	  num++;
	}
   }
}

When num will be equal to 5 then it will jump to the next condition without printing 5.

Output:

1
2
3
4
6
7
8
9
10

 

java java tutorials learn java study java