for loop in java

Java For Loop with Example | Java Tutorial

In this tutorial, we will about for loop in java with an example.

A for loop is a repetition control structure that allows you to design a loop that must be repeated a certain number of times quickly. When you know how many times a task will be repeated, a for loop comes in handy.

Where we used loops?

  • Loops are used in computer programming to repeat a section of code. If you want to repeat a message 100 times, for example, you can use a loop instead of inputting the same code 100 times.
  • The Java for loop is used to repeatedly iterate a section of the programme. If the number of iterations is fixed, the for loop is suggested.

There are three types of loops in Java.

  1. for loop
  2. while loop
  3. do…while loop

for loop in Java

A loop is used to repeatedly execute a set of statements until a certain condition is met. When showing numbers from 1 to 100, for example, you might wish to set the value of a variable to 1 and display it 100 times, raising its value by 1 with each loop iteration.

We can set the variable’s value, check the condition, and increment or decrement the value.

It is divided into four sections:

  1. Initialization: When the loop begins, the initial condition is executed once. We can either initialise the variable or use one that has already been initialised. It’s  an optional condition.
  2. Condition: This is the second condition, which is run every time the loop is tested. It keeps running until the condition is false. It must return either true or false as a boolean value. It is an optional condition.
  3. Increment/Decrement: It increases or decreases the value of the variable.It is an optional condition.
  4. Statement: The loop’s statement is executed each time the second condition is false.
for(initialization; condition; increment/decrement){    
//…statement or code to be executed
}  

Example:

//Java Program to show the example of for-loop.

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

    int D = 10;
    // for loop  
    for (int i = 1; i <= D; ++i) {
      System.out.println("Developers Dome");
    }
  }
}

Output:

Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome

working of for loop example shown above:

IterationVariableCondition: i <= DAction
1sti = 1
d = 10
trueDevelopers Dome is printed.
i is increased to 2.
2ndi = 2
d = 10
trueDevelopers Dome is printed.
i is increased to 3.
3rdi = 3
d = 10
true Developers Dome is printed.
i is increased to 4.
4thi = 4
d = 10
trueDevelopers Dome is printed.
i is increased to 5.
5thi = 5
d = 10
trueDevelopers Dome is printed.
i is increased to 6.
6thi = 6
d = 10
trueDevelopers Dome is printed.
i is increased to 7.
7thi = 7
d = 10
trueDevelopers Dome is printed.
i is increased to 8.
8thi= 8
d =10
trueDevelopers Dome is printed.
i is increased to 9.
9thi= 9
d = 10
trueDevelopers Dome is printed.
i is increased to 10.
10thi = 10
d = 10
trueDevelopers Dome is printed.
i is increased to 11.
11thi = 11
d = 10
false The loop is terminated.

Nested for Loop in Java

One loop is nested inside another in a nested loop. These are typically used for two-dimensional work. When one loop is nested inside another, the inner loop iterates many times inside the outer loop.

The term “nested for loop” refers to a for loop that is contained within another loop. When the outer loop executes, the inner loop executes completely.

public class Sample {
public static void main(String[] args) {  
// loop for i
for(int i=1;i<=2;i++){  
//loop for j  
for(int j=1;j<=2;j++){  
        System.out.println(i+" "+j);  
}//end of i  
}//end of j  
}  
}  

Output:

1 1
1 2
1 3
2 1
2 2
2 3

Pyramid Example using nested loop

public class Sample {  
public static void main(String[] args) {  
for(int i=1;i<=6;i++){  
for(int j=1;j<=i;j++){  
        System.out.print("* ");  
}  
System.out.println();// for new line  
}  
}  
}  

Output:

*
* *
* * *
* * * *
* * * * *
* * * * * *

Infinite for Loop in Java

// Infinite for Loop example

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

        for (int i = 1; i <= 10; --i) {
            System.out.println("Developers Dome");
        }
    }
}

In this case, the test expression, i<= 10, is never false, and Developers Dome is printed until memory runs out.

for loop java. for loop java for loop java for loop java for loop java for loop java for loop java for loop java for loop java for loop java

You May like:

Java switch Statement with Example | Java Tutorial

We hope that this article will assist you in understanding all about Java For Loop. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

This Post Has 4 Comments

Leave a Reply