For Loop Java

For Loop in Java

In this tutorial we will learn about for loop in Java. But before delving deep into for loop and how to use it, let’s understand the difference between the three types of loops. Loops in Java is a feature used to execute a particular part of the program repeatedly if a given condition evaluates to be true.

While all three types’ basic functionality remains the same, there’s a vast difference in the syntax and how they operate.

Looping is a very useful and important part of every programming language.In this tutorial, we will learn full functionality and working of for loop java.
Loops are used to perform a set of statements continusily until a particular condition is satisfied. We have the following types of loops.

1.) For Loop
2.) While Loop
3.) Do-While Loop

For loop syntax in Java

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:

for(initialization; condition; increment/decrement)
{    
//statement or code to be executed    
}

πŸ‘ͺ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.

πŸ‘ͺ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.

πŸ‘ͺIncrement/Decrement: It increments or decrements the variable value. It is an optional condition.

πŸ‘ͺStatement: The statement of the loop is executed each time until the second condition is false.

Example :

public class ForExample {  
public static void main(String[] args) {  
    //Code of Java for loop  
    for(int i=1;i<=10;i++){  
        System.out.println(i);  
    }  
} 


Output :

1
2
3
4
5
6
7
8
9
10

For each loop in Java

The for each loop in java 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

for each loop syntax

for(data_type variable : array_name){    
//code to be executed    
}

Example :

public class ForEachExample {  
public static void main(String[] args) {  
    //Declaring an array  
    int arr[]={12,24,69,78,6};  
    //Printing array using for-each loop  
    for(int i:arr){  
        System.out.println(i);  
    }  
}  
}


Output :

12
24
69
78
6

How to use break statement in for loop Java

The break in java for loop or break statement in Java programming language has the following two usages βˆ’

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

It can be used to terminate a case in the switch statement

Example :

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};
      for(int x : numbers ) {
         if( x == 30 ) {
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}


Output :

10
30

Different types of Java for loop

For Loop Java

Infinite Looper in Java

Sometimes according to requirement, we need to use Loop that will execute infinite times. In infinite loop, we write a condition that never return false.

Syntax :

for(;;){
//Statement that will execute infinite times
}

Example :

class MyFirstForLoopExample {
public static void main(String...s){
for( ; ; ){
System.out.println(" This text will be print infinite times ");
}
}
}

Output :

This text will be print infinite times
This text will be print infinite times
This text will be print infinite times
This text will be print infinite times....

Note: Above text will print infinite times.

Iterate Array using for Loop

An array is a collection of data with the same datatype. We have two ways to Iterate Array using for Loop.
a) Simple for loop

class MyFirstForLoopExample {
public static void main(String...s){
String a[]={"for loop java","foreach loop","java example programs"};
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
} 

b) Enhance for loop

class MyFirstForLoopExample {
public static void main(String...s){
String a[]={"for loop java","foreach loop","java example programs"};
for(String st:a){
System.out.println(st);
}
}
}

Note: The output of above both programs will be the same
for loop java foreach loop java example programs

Break for loop using the break keyword

break keyword is a very useful keyword in Java. We use the break keyword to stop the iteration of any loop before completing.

class MyFirstForLoopExample {
public static void main(String...s){
for(int i=0; i < 11; i++){
System.out.println("Value of i Variable is: "+ i );
if(i == 5){
break;
}
}
}
}
Output:
 Value of i Variable is:  0
 Value of i Variable is:  1
 Value of i Variable is:  2
 Value of i Variable is:  3
 Value of i Variable is:  4
 Value of i Variable is:  5

Nested for loop in Java

Nested word indicated one or more for loops inside the main for loop. Nested for loops are also useful to print some star pattern or number pattern in java. When we write for loop inside the another for loop that is called nested for loop.

Example :

class MyFirstForLoopExample {
public static void main(String...s){
for(int i=1; i < 5; i++){
System.out.println(" Value of first for loop "+ i );
for(int j=1;j<5;j++)
System.out.println(" Value of second for loop "+ j );
}
}
}

Output :

Value of first for loop 1
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4
Value of first for loop 2
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4
Value of first for loop 3
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4
Value of first for loop 4
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4

More Useful Tutorials are:

1.) Login Page in PHP
2.) How to Add Google Analytics Code in WordPress
3.) How to Install Laravel and Its Configuration
4.) Java Split String

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!