For Loop Java

For Loop in Java

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 Java

For Loop Java

1.) For Loop Syntax in Java

for(initialization; test condition; increment or decrement){
Statement or Statements;
}

In the above for loop syntax, we have used follwing three terms.

a) Initialization : It means to initialize some value to variable.
Example: int i=0; i.e “i” is just variable we can take any one.

b.) Test Condition : It will test the condition upto for loop will execute.
Example: i < 11;

c.) Increment or Decrement : Here we will increase or decrease given variable (in our case: i).
Examaple: i++ orĀ  i – – ;

Example of for loop using above syntax:

class MyFirstForLoopExample {
public static void main(String...s){
for(int i=0; i < 11; i++){
System.out.println(" Value of i Variable is: "+ i );
}
}
}

Output of above Example is:

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
Value of i Variable is: 6
Value of i Variable is: 7
Value of i Variable is: 8
Value of i Variable is: 9
Value of i Variable is: 10

2.) 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.

Infinite For Loop Syntax in Java

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.

3.) 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

4.) 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

5.) Nested for loops 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 *

0 0 0
FOLLOW ON