What is for loop in Java?
A for loop in Java is a control flow statement used to repeatedly execute a block of code a fixed number of times. It consists of an initialization expression, a test expression, an update expression, and a block of code to be executed. It is commonly used for iterating over arrays or collections of data.
For Loop Syntax
for (initialization; condition; increment/decrement) { statements; // code to be executed repeatedly }
- The initializer is used to initialize the loop control variable.
- The condition is checked each time before execution of the statements. If the condition evaluates to true, then the statements are executed. And if the condition evaluates to false, then execution of the statements is terminated.
- The increment/decrement step can be used to change the value of the control variable each time through the loop.
Write a Java Program to Display Numbers from 1 to 10 using For Loop.
We use For Loop in which we initialise a variable to 1 and increments each time by 1 till we reach 10. The loop breaks when variable attains value 11.
In this method, we will display numbers from 1 to 10 using For Loop.
Here is the source code of the Java Program to Display Numbers from 1 to 10 Using For Loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/* * Java Program to Print 1 to 10 Numbers using For Loop */ public class For_Loop { public static void main(String[] args) { for(int i = 1; i <= 10; i++) { System.out.println(i); } } }
1. The program declares a public class named “For_Loop“.
2. A for loop is declared inside the “main” method with three parts: initialization, condition, and update.
3. The initialization part initializes an integer variable “i” to 1.
4. The condition part specifies the loop to continue while “i” is less than or equal to 10.
5. The update part increments “i” by 1 in each iteration of the loop.
6. The “System.out.println()” statement is used to print the value of “i” to the console.
7. The “println()” method prints the value followed by a newline character to print each number on a separate line.
8. When executed, the program outputs the numbers 1 to 10 on separate lines.
$ javac Use_For_Loop.java $ java Use_For_Loop 1 2 3 4 5 6 7 8 9 10
A nested for loop in Java is a loop structure where one for loop is placed inside another. The outer loop is executed first, followed by the inner loop.
Nested For Loop Syntax
for (initialization; condition; increment/decrement) { for (initialization; condition; increment/decrement) { // Code to be executed in inner loop } // Code to be executed in outer loop }
- Nested for loops have an outer loop containing an inner loop.
- Each loop has initialization, condition, and increment/decrement statements.
- The inner loop is enclosed in curly braces within the outer loop.
- The code inside the inner loop executes entirely for each iteration of the outer loop.
- The initialization, condition, and increment/decrement statements are defined according to the task or data structure being traversed.
Here is the source code of the Java Program to Display Numbers from 1 to 10 Using Nested For Loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Nested_For_Loop { public static void main(String[] args) { int count = 1; for(int i = 1; i <= 2; i++) // outer loop for rows { for(int j = 1; j <= 5; j++) // inner loop for columns { System.out.print(count + " "); // print the count variable count++; } System.out.println(); } } }
1. The Java program prints a sequence of numbers using nested for loops in a 2×5 grid.
2. The program starts by setting a variable ‘count‘ to 1.
3. The outer loop runs for two rows, and the inner loop runs for five columns.
4. Inside the inner loop, the program prints the value of the ‘count‘ variable, then adds a space.
5. After printing the value, the ‘count‘ variable is incremented for the next iteration.
6. After completing the inner loop for each row, the outer loop moves to the next row.
7. Finally, the program prints a new line to move to the next row using the ‘println‘ statement.
$ javac Use_For_Loop.java $ java Use_For_Loop 1 2 3 4 5 6 7 8 9 10
To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.
- Apply for Java Internship
- Check Programming Books
- Check Java Books
- Apply for Computer Science Internship
- Practice Information Technology MCQs