There are several looping statements available in java. One of them is for loop in java.
For loop
in java is used to execute some statements repeatedly until the condition returns false
.
There are three parts to for loop.
- Initialization
- Condition
- Increment or decrement
Table of Contents
Syntax for for loop in java
1 2 3 4 5 |
for (initialization; condition; increment/decrement) { //block of statements } |
Initialization: Initialization statement executes at beginning of loop only once.
condition:Condition gets evaluated in each iteration. For loop executes block of statements repeatedly unless condition returns false
.
Increment/Decrement: These statements get executed in each iteration.
Let’s take a very simple example
Print number from 1 to 10 using for loop
1 2 3 4 5 6 7 8 9 10 |
public class ForLoopMain { public static void main(String[] args) { for (int i = 1; i < 11; i++) { System.out.print(" "+i); } } } |
Output:
If you are still confused with above concept, let’s understand it with the help of flow diagram.
Let’s try to print only even numbers now.
1 2 3 4 5 6 7 8 9 10 11 |
public class ForLoopMain { public static void main(String[] args) { for (int i = 1; i < 11; i++) { if(i%2==0) System.out.print(" "+i); } } } |
Output:
Exercise
If you are given array of integer, you need to find an element in that array.
Input:
{32,45,53,65,43,23}
You need to write a program to search an element in the array
. If the element is found in the array, return "PRESENT" else return "NOT PRESENT"
I would recommend you try it yourself and then look at the below code.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; public class ForStatementExample { public static void main(String[] args) { ForStatementExample bse=new ForStatementExample(); int arr[] ={32,45,53,65,43,23}; System.out.println(bse.findElementInArr(arr, 53)); } public String findElementInArr(int arr[],int elementTobeFound) { for (int i = 0; i < arr.length; i++) { if(arr[i]==elementTobeFound) { System.out.println(elementTobeFound+" is present in the array "); return "PRESENT"; } } return "NOT PRESENT"; } } |
Output:
PRESENT
Infinite for loop in java
You need to be careful with condition you provide in for loop otherwise you may end up creating infinite for loop.
For example:
Let’s say you want to print number from 10 to 1 and you use below code:
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; public class ForLoopMain { public static void main(String[] args) { for (int i = 10; i >0; i--) { System.out.print(" "+i); } } } |
Output:
Now in above code, instead of i–, you have put i++. In this code, loop will go into infinite loop.
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; public class ForLoopMain { public static void main(String[] args) { for (int i = 10; i >0; i++) { System.out.print(" "+i); } } } |
Another example of inifinite loop is below code:
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; package org.arpit.java2blog; public class ForLoopMain { public static void main(String[] args) { for (; ; ) { } } } |
Use two variables in for loop in java
You can use multiple variables in for loop too.
For example:
Let’s say you want to check if String is palindrome or not. You can use two variables in for loop as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package org.arpit.java2blog; import java.util.Scanner; public class StringUsingHalfLoop { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a string : "); String str = scanner.nextLine(); boolean isPalin=isPalindrome(str); if(isPalin) System.out.println("String is Palindrome"); else System.out.println("String is not Palindrome"); } static boolean isPalindrome(String str) { for (int i = 0,j=str.length()-1; i <str.length()/2; i++,j--) { if(str.charAt(i)!=str.charAt(j)) { return false; } } return true; } } |
Output:
String is Palindrome
Enter a string : adam
String is not Palindrome
That’s all about for loop in java.