The For Loop
When you need to iterate over a block of code a fixed number of times, the for statement provides a good deal of flexibility. In essence, you are able to specify how many times a block of code repeats itself, as well as the terminating condition.
Without belaboring the point, here is a sample of the syntax:
1 2 3 4 5 6 |
for (initialization; testExpression; update) { // codes inside for loop's body } |
All your old C and C++ tricks still hold when building a Java for statement. You can create complex terminating conditions, build endless loops, loop in reverse (via the — operator), and use the goto, continue, and break jump keywords.
Java for Loop Examples
Example 1: Program to print a sentence 10 times.
1 2 3 4 5 6 7 8 9 10 |
class ForLoop { public static void main(String[] args) { for (int i = 1; i <= 10; ++i) { System.out.println("Line " + i); } } } |
Example 2: Program to find the sum of natural numbers from 1 to 1000.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class ForLoop{ public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 1000; ++i) { sum += i; // sum = sum + i } System.out.println("Sum = " + sum); } } |
Example 3: Program to print the numbers from 10 to 1.
1 2 3 4 5 6 7 8 9 |
class ForLoop{ public static void main(String args[]){ for(int i=10; i>1; i--){ System.out.println("The value of i is: "+i); } } } |
Example 4: Printing Odd numbers between 1 and limit that is given by user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class forLoop{ public static void main(String[] args) { //define the limit int limit = 50; System.out.println("Printing Odd numbers between 1 and " + limit); for(int i=1; i <= limit; i++){ //if the number is not divisible by 2 then it is odd if( i % 2 != 0){ System.out.print(i + " "); } } } } |
Example 5: Printing Even numbers between 1 and limit that is given by user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class forLoop{ public static void main(String[] args) { //define limit int limit = 50; System.out.println("Printing Even numbers between 1 and " + limit); for(int i=1; i <= limit; i++){ // if the number is divisible by 2 then it is even if( i % 2 == 0){ System.out.print(i + " "); } } } } |
Example 6: Infinite for Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Infinite for Loop class forLoop{ public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 10; --i) { System.out.println("Hello"); } } } |
Example 7: Calculate Average value of Array elements
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 |
public class ForLoop{ public static void main(String[] args) { //define an array int[] numbers = new int[]{10,20,15,25,16,60,100}; /* * Average value of array elements would be * sum of all elements/total number of elements */ //calculate sum of all array elements int sum = 0; for(int i=0; i < numbers.length ; i++) sum = sum + numbers[i]; //calculate average value double average = sum / numbers.length; System.out.println("Average value of array elements is : " + average); } } |
Example 8: Declare multiple variables in for loop
1 2 3 4 5 6 7 8 9 10 11 |
public class ForLoop{ public static void main(String[] args) { for(int i=0, j=1, k=2 ; i<5 ; i++) System.out.println("I : " + i + ",j : "+ j + ", k : " + k); } } |
Example 9: Fibonacci Series Java Example
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 |
public class FibonacciSeries { public static void main(String[] args) { //number of elements to generate in a series int limit = 20; long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; //create the Fibonacci series and store it in an array for(int i=2; i < limit; i++){ series[i] = series[i-1] + series[i-2]; } //print the Fibonacci series numbers System.out.println("Fibonacci Series upto " + limit); for(int i=0; i< limit; i++){ System.out.print(series[i] + " "); } } } |
Example 10: Generate Pyramid For a Given Number
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 |
import java.io.BufferedReader; import java.io.InputStreamReader; public class ForLoop { public static void main (String[] args) throws Exception{ BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in)); System.out.println("Enter Number:"); int as= Integer.parseInt (keyboard.readLine()); System.out.println("Enter X:"); int x= Integer.parseInt (keyboard.readLine()); int y = 0; for(int i=0; i<= as ;i++){ for(int j=1; j <= i ; j++){ System.out.print(y + "t"); y = y + x; } System.out.println(""); } } } |
Example 11: For loop example to iterate an array
1 2 3 4 5 6 7 8 9 10 11 |
class ForLoopExample { public static void main(String args[]){ int arr[]={2,11,45,9}; //i starts with 0 as array index starts with 0 too for(int i=0; i<arr.length; i++){ System.out.println(arr[i]); } } } |
Example 12: Finding factorial using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class JavaForLoopExample { public static void main(String[] args) { //We will find the factorial of this number int number = 5; long fact = 1; for(int i = 1; i <= number; i++) { fact = fact * i; } System.out.println("Factorial of "+number+" is: "+fact); } } |
Example 13: Program to calculate the sum of natural numbers using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class JavaLoopExample{ public static void main(String[] args) { int num = 10, count, total = 0; for(count = 1; count <= num; count++){ total = total + count; } System.out.println("Sum of first 10 natural numbers is: "+total); } } |
Example 14: Java Palindrome Number Example
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 |
public class JavaPalindromeNumberExample { public static void main(String[] args) { //array of numbers to be checked int numbers[] = new int[]{121,13,34,11,22,54}; //iterate through the numbers for(int i=0; i < numbers.length; i++){ int number = numbers[i]; int reversedNumber = 0; int temp=0; //reverse the number while(number > 0){ temp = number % 10; number = number / 10; reversedNumber = reversedNumber * 10 + temp; } if(numbers[i] == reversedNumber) System.out.println(numbers[i] + " is a palindrome number"); else System.out.println(numbers[i] + " is not a palindrome number"); } } } |
Example 15: Java Pyramid 1 Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class JavaPyramid { public static void main(String[] args) { for(int i=1; i<= 5 ;i++){ for(int j=0; j < i; j++){ System.out.print("*"); } System.out.println(""); } } } |
Example 16: Java Pyramid 2 Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class JavaPyramid { public static void main(String[] args) { for(int i=5; i>0 ;i--){ for(int j=0; j < i; j++){ System.out.print("*"); } System.out.println(""); } } } |
Example 17: Java Pyramid 3 Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class JavaPyramid { public static void main(String[] args) { for(int i=1; i<= 5 ;i++){ for(int j=0; j < i; j++){ System.out.print(j+1); } System.out.println(""); } } } |
Example 18: Java Pyramid 4 Example
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 |
public class JavaPyramid { public static void main(String[] args) { //generate upper half of the pyramid for(int i=5; i>0 ;i--){ for(int j=0; j < i; j++){ System.out.print("*"); } //create a new line System.out.println(""); } //generate bottom half of the pyramid for(int i=1; i<= 5 ;i++){ for(int j=0; j < i; j++){ System.out.print("*"); } //create a new line System.out.println(""); } } } |