Search Here

Loops in Java

 The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.


There are three types of for loops in Java.


  • Simple for Loop
  • For-each or Enhanced for Loop
  • Labeled for Loop

Java Simple for Loop

A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:

  1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition.
  2. Condition: It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
  3. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
  4. Statement: The statement of the loop is executed each time until the second condition is false.

Syntax:

  1. or(initialization; condition; increment/decrement){    
  2. //statement or code to be executed    
  3. }    

Flowchart:

for loop in java flowchart

Example:

ForExample.java

  1. //Java Program to demonstrate the example of for loop  
  2. //which prints table of 1  
  3. public class ForExample {  
  4. public static void main(String[] args) {  
  5.     //Code of Java for loop  
  6.     for(int i=1;i<=10;i++){  
  7.         System.out.println(i);  
  8.     }  
  9. }  
  10. }  

Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java Nested for Loop

If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.

Example:

NestedForExample.java

  1. public class NestedForExample {  
  2. public static void main(String[] args) {  
  3. //loop of i  
  4. for(int i=1;i<=3;i++){  
  5. //loop of j  
  6. for(int j=1;j<=3;j++){  
  7.         System.out.println(i+" "+j);  
  8. }//end of i  
  9. }//end of j  
  10. }  
  11. }  

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Pyramid Example 1:

PyramidExample.java

  1. public class PyramidExample {  
  2. public static void main(String[] args) {  
  3. for(int i=1;i<=5;i++){  
  4. for(int j=1;j<=i;j++){  
  5.         System.out.print("* ");  
  6. }  
  7. System.out.println();//new line  
  8. }  
  9. }  
  10. }  

Output:

* 
* * 
* * * 
* * * * 
* * * * * 

Pyramid Example 2:

PyramidExample2.java

  1. public class PyramidExample2 {  
  2. public static void main(String[] args) {  
  3. int term=6;  
  4. for(int i=1;i<=term;i++){  
  5. for(int j=term;j>=i;j--){  
  6.         System.out.print("* ");  
  7. }  
  8. System.out.println();//new line  
  9. }  
  10. }  
  11. }  

Output:

* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*  

Java for-each Loop

The for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.

It works on the basis of elements and not the index. It returns element one by one in the defined variable.

Syntax:

  1. for(data_type variable : array_name){    
  2. //code to be executed    
  3. }    

Example:

ForEachExample.java

  1. //Java For-each loop example which prints the  
  2. //elements of the array  
  3. public class ForEachExample {  
  4. public static void main(String[] args) {  
  5.     //Declaring an array  
  6.     int arr[]={12,23,44,56,78};  
  7.     //Printing array using for-each loop  
  8.     for(int i:arr){  
  9.         System.out.println(i);  
  10.     }  
  11. }  
  12. }  

Test it Now

Output:

12
23
44
56
78

Java Labeled For Loop

We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the nested for loop as we can break/continue specific for loop.

Note: The break and continue keywords breaks or continues the innermost for loop respectively.

Syntax:

  1. labelname:    
  2. for(initialization; condition; increment/decrement){    
  3. //code to be executed    
  4. }    

Example:

LabeledForExample.java

  1. //A Java program to demonstrate the use of labeled for loop  
  2. public class LabeledForExample {  
  3. public static void main(String[] args) {  
  4.     //Using Label for outer and for loop  
  5.     aa:  
  6.         for(int i=1;i<=3;i++){  
  7.             bb:  
  8.                 for(int j=1;j<=3;j++){  
  9.                     if(i==2&&j==2){  
  10.                         break aa;  
  11.                     }  
  12.                     System.out.println(i+" "+j);  
  13.                 }  
  14.         }  
  15. }  
  16. }  

Output:

1 1
1 2
1 3
2 1

If you use break bb;, it will break inner loop only which is the default behaviour of any loop.

LabeledForExample2.java

  1. public class LabeledForExample2 {  
  2. public static void main(String[] args) {  
  3.     aa:  
  4.         for(int i=1;i<=3;i++){  
  5.             bb:  
  6.                 for(int j=1;j<=3;j++){  
  7.                     if(i==2&&j==2){  
  8.                         break bb;  
  9.                     }  
  10.                     System.out.println(i+" "+j);  
  11.                 }  
  12.         }  
  13. }  
  14. }  

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Infinitive for Loop

If you use two semicolons ;; in the for loop, it will be infinitive for loop.

Syntax:

  1. for(;;){  
  2. //code to be executed  
  3. }  

Example:

ForExample.java

  1. //Java program to demonstrate the use of infinite for loop  
  2. //which prints an statement  
  3. public class ForExample {  
  4. public static void main(String[] args) {  
  5.     //Using no condition in for loop  
  6.     for(;;){  
  7.         System.out.println("infinitive loop");  
  8.     }  
  9. }  
  10. }  

Output:

infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c

Now, you need to press ctrl+c to exit from the program.

Java for Loop vs while Loop vs do-while Loop

Comparisonfor loopwhile loopdo-while loop
IntroductionThe Java for loop is a control flow statement that iterates a part of the programs multiple times.The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.
When to useIf the number of iteration is fixed, it is recommended to use for loop.If the number of iteration is not fixed, it is recommended to use while loop.If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.
Syntaxfor(init;condition;incr/decr){
// code to be executed
}
while(condition){
//code to be executed
}
do{
//code to be executed
}while(condition);
Example//for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
//while loop
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
//do-while loop
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
Syntax for infinitive loopfor(;;){
//code to be executed
}
while(true){
//code to be executed
}
do{
//code to be executed
}while(true);



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section