Tuesday 28 June 2016

Continue Statement in Java

Java Continue Statement

The Java continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. In case of inner loop, it continues only inner loop.
Syntax:
  1. jump-statement;    
  2. continue;   

Java Continue Statement Example

Example:
  1. public class ContinueExample {  
  2. public static void main(String[] args) {  
  3.     for(int i=1;i<=10;i++){  
  4.         if(i==5){  
  5.             continue;  
  6.         }  
  7.         System.out.println(i);  
  8.     }  
  9. }  
  10. }  
Output:
1
2
3
4
6
7
8
9
10

Java Continue Statement with Inner Loop

It continues inner loop only if you use continue statement inside the inner loop.
Example:
  1. public class ContinueExample2 {  
  2. public static void main(String[] args) {  
  3.             for(int i=1;i<=3;i++){    
  4.                     for(int j=1;j<=3;j++){    
  5.                         if(i==2&&j==2){    
  6.                             continue;    
  7.                         }    
  8.                         System.out.println(i+" "+j);    
  9.                     }    
  10.             }    
  11. }  
  12. }  
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
 

Break Statement in Java

Java Break Statement

The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.
Syntax:
  1. jump-statement;    
  2. break;   
java break statement flowchart

Java Break Statement with Loop

Example:
  1. public class BreakExample {  
  2. public static void main(String[] args) {  
  3.     for(int i=1;i<=10;i++){  
  4.         if(i==5){  
  5.             break;  
  6.         }  
  7.         System.out.println(i);  
  8.     }  
  9. }  
  10. }  
Output:
1
2
3
4

Java Break Statement with Inner Loop

It breaks inner loop only if you use break statement inside the inner loop.
Example:
  1. public class BreakExample2 {  
  2. public static void main(String[] args) {  
  3.             for(int i=1;i<=3;i++){    
  4.                     for(int j=1;j<=3;j++){    
  5.                         if(i==2&&j==2){    
  6.                             break;    
  7.                         }    
  8.                         System.out.println(i+" "+j);    
  9.                     }    
  10.             }    
  11. }  
  12. }  
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Break Statement with Switch

To understand the example of break with switch statement, please visit here: Java Switch Statement.

For-each loop (Advanced or Enhanced For loop):

 

For-each loop (Advanced or Enhanced For loop):

The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.

Advantage of for-each loop:

  • It makes the code more readable.
  • It elimnates the possibility of programming errors.

Syntax of for-each loop:

  1. for(data_type variable : array | collection){}  

Simple Example of for-each loop for traversing the array elements:

  1.     
  2. class ForEachExample1{  
  3.   public static void main(String args[]){  
  4.    int arr[]={12,13,14,44};  
  5.   
  6.    for(int i:arr){  
  7.      System.out.println(i);  
  8.    }  
  9.   
  10.  }   
  11. }  
  12.       
Test it Now
Output:12
       13
       14
       44

Simple Example of for-each loop for traversing the collection elements:

  1. import java.util.*;  
  2. class ForEachExample2{  
  3.   public static void main(String args[]){  
  4.    ArrayList<String> list=new ArrayList<String>();  
  5.    list.add("vimal");  
  6.    list.add("sonoo");  
  7.    list.add("ratan");  
  8.   
  9.    for(String s:list){  
  10.      System.out.println(s);  
  11.    }  
  12.   
  13.  }   
  14. }  
  15.       
Test it Now
Output:vimal
       sonoo
       ratan

do-while Loop in Java

Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use while loop.


It is executed at least once because condition is checked after loop body.

Like a while statement, except that it tests the condition at the end of the loop body

Syntax:
  1. do{  
  2. //code to be executed  
  3. }while(condition);  
flowchart of do while loop in java
Example:
  1. public class DoWhileExample {  
  2. public static void main(String[] args) {  
  3.     int i=1;  
  4.     do{  
  5.         System.out.println(i);  
  6.     i++;  
  7.     }while(i<=10);  
  8. }  
  9. }  
Output:
1
2
3
4
5
6
7
8
9
10

Java Infinitive do-while Loop

If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
  1. while(true){  
  2. //code to be executed  
  3. }  
Example:
  1. public class DoWhileExample2 {  
  2. public static void main(String[] args) {  
  3.     do{  
  4.         System.out.println("infinitive do while loop");  
  5.     }while(true);  
  6. }  
  7. }  
Output:
infinitive do while loop
infinitive do while loop
infinitive do while loop
ctrl+c
Now, you need to press ctrl+c to exit from the program.