Tuesday, 28 June 2016

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.


While Loop in Java

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.
Syntax:
  1. while(condition){  
  2. //code to be executed  
  3. }  
flowchart of java while loop
Example:
  1. public class WhileExample {  
  2. public static void main(String[] args) {  
  3.     int i=1;  
  4.     while(i<=10){  
  5.         System.out.println(i);  
  6.     i++;  
  7.     }  
  8. }  
  9. }  
Output:
1
2
3
4
5
6
7
8
9
10

Java Infinitive While Loop

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

A while loop statement in java programming language repeatedly executes a target statement as long as a given condition is true.

Syntax:

The syntax of a while loop is:
while(Boolean_expression)
{
   //Statements
}
Here, statement(s) may be a single statement or a block of statements. Thecondition may be any expression, and true is any non zero value.
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
When the condition becomes false, program control passes to the line immediately following the loop

Flow Diagram

Java Tutorial
Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.