Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

For Loop Program In Java

We’ll learn how to create and use a for loop in Java in this post. We will also study how a for loop functions in computer programming.

Loops are used in computer programming to repeatedly run a block of code. Instead of inputting the same code 100 times, you might use a loop, for instance, to display the same message 100 times.

In Java, there are three types of loops.

  • for loop
  • while loop
  • do…while loop

We will discuss for loop in detail in this article.

for Loop in Java

To run a block of code a certain number of times, use a Java for loop. The for loop’s syntax is:

for (initialExpression; testExpression; updateExpression)
{
    // body of the loop
}

Here,

  • InitialExpression only ever executes once while initialising and/or declaring variables.
  • The condition is examined. The for loop’s body is executed if the condition is satisfied.
  • The initialExpression’s value is updated by the updateExpression.
  • The condition is once more evaluated. Until the condition is false, the process keeps going.

Example 1: Display a Text Five Times

class prepbytes
{
public static void main(String[] args) {

    int n = 5;
    for (int i = 1; i <= n; ++i) {
      System.out.println("PrepBytes");
    }
  }
}
Output
PrepBytes
PrepBytes
PrepBytes
PrepBytes
PrepBytes

Here is how this program works.

Iteration Variable Condition: i <= n Action
1st i = 1, n = 5 true PrepBytes  is printed.
i is increased to 2.
2nd i = 2
n = 5 true PrepBytes is printed

i is increased to 3. |
| 3rd | i = 3

n = 5 | true | PrepBytes  is printed.

i is increased to 4. |
| 4th | i = 4

n = 5 | true | PrepBytes  is printed.

i is increased to 5. |
| 5th | i = 5

n = 5 | true | PrepBytes  is printed.

i is increased to 6. |
| 6th | i = 6

n = 5 | false | The loop is terminated. |

Example 2: Display numbers from 1 to 5

class Main {
  public static void main(String[] args) {
  
    int n = 5;
    for (int i = 1; i <= n; ++i) {
      System.out.println(i);
    }
  }
}
Output
1
2
3
4
5

Example 3: Display Sum of n Natural Numbers

class Main {
  public static void main(String[] args) {
      
    int sum = 0;
    int n = 1000;

    
    for (int i = 1; i <= n; ++i) {
      sum += i;
    }
       
    System.out.println("Sum = " + sum);
  }
}
Output
Sum = 500500

Here, the value of sum is 0 initially. Then, the for loop is iterated from i = 1 to 1000. In each iteration, it is added to the sum and its value is increased by 1.

When i becomes 1001, the test condition is false and the sum will be equal to 0 + 1 + 2 + …. + 1000.

for-each Loop in Java

The Java for loop has an alternative syntax that makes it easy to iterate through arrays and collections. For example,

class Main {
  public static void main(String[] args) {      
    int[] numbers = {2, 4, 6, 8};
    for (int number: numbers) {
       System.out.println(number);
    }
  }
}
Output
2
4
6
8

Here, we have used the for-each loop to print each element of the numbers array one by one.

In the first iteration of the loop, number will be 3, number will be 7 in second iteration and so on.

Infinite for Loop in Java

If we set the test expression in such a way that it never evaluates to false, the for loop will run forever. This is called an infinite for loop. For example,

class Infinite {
    public static void main(String[] args) {      
        int sum = 0;
        for (int i = 1; i <= 10; --i) {
            System.out.println("PrepBytes");
        }
    }
}

Here, the test expression ,i <= 10, is never false and PrepBytes is printed repeatedly until the memory runs out.

In this article, we tried to explain the implementation of for loop in java with the help of different examples. Loops are the basics of any language which must be a must to practice for becoming a master in any language.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java

Leave a Reply

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