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.
Contents
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.
- for loop
- while loop
- 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:
- 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.
- 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.
- Increment/Decrement: It increases or decreases the value of the variable.It is an optional condition.
- 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:
Iteration | Variable | Condition: i <= D | Action |
---|---|---|---|
1st | i = 1 d = 10 | true | Developers Dome is printed. i is increased to 2. |
2nd | i = 2 d = 10 | true | Developers Dome is printed. i is increased to 3. |
3rd | i = 3 d = 10 | true | Developers Dome is printed. i is increased to 4. |
4th | i = 4 d = 10 | true | Developers Dome is printed. i is increased to 5. |
5th | i = 5 d = 10 | true | Developers Dome is printed. i is increased to 6. |
6th | i = 6 d = 10 | true | Developers Dome is printed. i is increased to 7. |
7th | i = 7 d = 10 | true | Developers Dome is printed. i is increased to 8. |
8th | i= 8 d =10 | true | Developers Dome is printed. i is increased to 9. |
9th | i= 9 d = 10 | true | Developers Dome is printed. i is increased to 10. |
10th | i = 10 d = 10 | true | Developers Dome is printed. i is increased to 11. |
11th | i = 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.
Pingback: Java while loop and do-while loop - Developers Dome
Pingback: Continue Statement in Java | Java Tutorial - Developers Dome
Pingback: Java Program to Display Fibonacci Series using loops with Example
Pingback: Java Program to find Sum of Natural Numbers with Example