Hey there! Before reading the article which is about For Loop in Java | Example Program, you should read about java loop programs for practice. It shares basic information about this article and also adds more value to it. Do let us know how you feel about it in the comment section below. Happy Reading! Really glad that you are here.
The For Loop in Java is a control structure that allows you to repeat a code block a specific number of times.
For Loop in Java A loop is a programming statement that repeats code until a certain condition occurs. A loop statement can be in the form of a while loop, a do while loop, a for loop or a for each loop. A Java program can have any number of loops in a program. This article will discuss about the for loop and the for each loop.
A for-loop in Java is an input-driven loop structure that executes a series of statements a fixed number of times.
The for statement provides a more concise syntax for creating loops. It executes the instruction block as long as the condition is true.
The general syntax of the for loop in Java is as follows:
for(initialization; test condition; iteration(increase/decrease)) {
// the body of the loop
instruction(s);
}
Or,
for (i = initialValue; i < endValue; i++) {
// the body of the loop
instruction(s);
}
Figure (a) shows a block diagram of the for-loop operator.
In the above syntax, the loopfor statement begins with the for keyword, followed by a pair of parentheses that enclose the loop’s control structure.
This structure consists of an initialization, a control condition and an iteration (increment or decrement). This control structure is followed by a loop surrounded by braces.
The initialization, test condition, and iteration are separated by semicolons. There is no semicolon at the end of the iteration section.
The procedure for is as follows:
1. Initialization of control variables : A for loop typically uses a variable to determine how many times the loop is executed and when it ends.
This variable is called the control variable. It is initialized with an assignment operator, for example. B. i = 1 and count = 0. The variables i and count are called control variables of the loop. It is executed only once when the for command is executed.
2. Test conditions: The value of the manipulated variable is estimated using a test condition. The test condition is a boolean expression, e.g. B. i < 10, which determines when the loop ends.
It is evaluated before each iteration (run) of the cycle. If the condition is true, the core of the loop is executed. If it is false, the loop is interrupted and execution continues until the next instruction after the parenthesis immediately following the loop.
3. Iteration: When the body of the loop is executed, control is returned to the for instruction after the last instruction in the loop is executed.
The manipulated variable is increased or decreased using assignment operators such as i++ (i.e. i = i + 1) or i- (i.e. i = i – 1) and the new value of the manipulated variable is re-evaluated to check whether it satisfies the loop condition or not.
If the test condition is met, the loop is executed again. This process continues until the value of the manipulated variable meets the test condition.
Consider the following program code.
int i;
for(i = 1; i <= 10; i++ ){
System.out.println(Hello Java);
}
Let’s see how the for loop works in the above example. A block diagram of the application is shown in figure (b). The for loop is initially set to 1, and the expression i = 1 is executed only once for the first time.
The test condition is a boolean expression. The expression (i <= 10) is checked immediately after initialization and at the beginning of each iteration. If this condition is true, the statement is executed inside the loop and Hello Java is printed.
The expression (i++) is executed. Now the value of i becomes 2. Since the value of i is less than 10, the operator is executed and Hello Java is printed again.
This process continues until the value of i is greater than 10. If it is greater than 10, the expression (i <= 10) becomes false and the loop ends.
Now, as we understood from the discussion above, the expression (i = 1) is executed only once at the beginning of the loop. Then the expressions (i <= 10) and (i++) are executed until i becomes less than or equal to 10.
If there is only one operator in the loop, as in this example, the braces can be omitted.
Example of a program based on the for loop instruction
1. Let’s take the example of a program in which we want to represent the numbers from 1 to 5 and from 5 to 1.
Program source code 1 :
package javaProgram;
public class Test {
public static void main(String[] args)
{
System.out.println(Display numbers from 1 to 5 :));
for(int i = 1; i <=5; i++){ // Here i++ is the increment operator. System.out.println(i); } System.out.println(Show numbers from 5 to 1:); for(int j = 5; j > 0; j–){ // Here j– is the operator for decrementing.
System.out.println(j);
}
}
}
Exit:
Displays numbers from 1 to 5:
1
2
3
4
5
Displays the numbers from 5 to 1 :
5
4
3
2
1
We can also write the same for-loop as follows
int i = 1;
for(; i <= 5;){
System.out.println(i);
i++;
}
2. Consider another program in which we want to calculate the sum of squares of integers from 1 to 5 using the for-loop operator. For a better understanding, take a look at the following source code.
Program source code 2 :
package javaProgram;
public class Test {
public static void main(String[] args)
{
int i = 1;
int sum = 0;
for(; i <= 5;){
sum = sum + i*i;
i++;
}
System.out.println(sum : +sum);
}
}
Infinite loop for Java
Consider the following example of program code.
int x = 1;
for( ; 😉
{
System.out.println(x);
x++;
}
In this example, there is no check condition in the for statement that specifies where to stop. This will allow the code to run without interruption. This loop is called an infinite loop in Java. We must avoid an infinite loop.
If we accidentally create an infinite for loop in a Java program, we can end it with the break operator. It can be used to get out of the loop.
3. Let’s take an example of a program based on an infinite for-loop, where we sum the cubes of the numbers from 1 to 5.
Program source code 3 :
package javaProgram;
public class Test {
public static void main(String[] args)
{
int i = 1;
int sum = 0;
for(;;){
sum = sum + i*i*i;
i++;
if(i >= 5) break; // If i is greater than 5, exit the loop
}System.out.println(Sum: +sum);}
4. Consider an example program in which we initialize two variables in the for statement and simultaneously represent the numbers 1 to 5 and 5 to 1.
Program source code 4 :
package javaProgram;public class Test {public static void main(String[] args){int i, j;for(i = 1, j = 5; i <= 5; i++, j–){System.out.println(i+ t +j);}}
Exit:
1 5
2 4
3 3
4 2
5 1
In this loop, two initialization expressions (i = 1, and j = 5) and two iteration expressions (i++, and j-) were used. However, there is only one test condition expression (i < = 5).
In this for-loop, the values of i are executed from 1 to 5, while at the same time the values of j go from 5 to 1.
Message:
1. A for loop can have more than one initialization expression and one iteration expression. This function cannot be used in other hinges. Each expression must be separated from the next by a comma.
Let’s look at another program based on this point.
Program source code 5 :
package javaProgram ;public class Test {public static void main(String[] args){int x, y ;for(x = 1, y = 5; x < y; x++, y–){System.out.println(x = + x) ;System.out.println(y = + y) ;}}
Output:
x = 1
y = 5
x = 2
y = 4
In this example program, the initialization portion of the program consists of two control variables x and y, separated by a comma. Two comma separated expressions (x++ and y-) in the iteration part are executed each time the loop is repeated.
2. A test condition in the for-loop operator can also have a compound relationship. Let’s look at a sample program.
Program source code 6 :
package javaProgram;
public class Test {
public static void main(String[] args)
{
int x = 1, sum = 0;
for(x = 1; x < 20 && sum < 20; x++){
sum = sum + x;
}
System.out.println(Sum: +sum);
}
}
In this example, the loop is executed until both conditions x < 20 and sum < 20 are met. The amount is assessed in the body of the cycle.
3. You can also use expressions in the initialization and incrementation sections. For example, this type of statement is perfectly acceptable for(x = (a + b)/5; x > 10; x = x/5).
4. If we declare a control variable within a for statement, the range of that variable ends when the for loop ends. This means that the control variable defined in the for statement is a local variable that cannot be accessed from outside the loop.
Program source code 7 :
public class Test {
public static void main(String[] args)
{
for(int x = 1; x < 10; x++){
System.out.println(x); // No error.
}
System.out.println(x); // Compilation error.
}
}
I hope this tutorial has covered all the important points about the for loop in Java with several example programs. I hope you understood this simple topic and practiced Java for loop-based programs.
Thanks for reading!!!
This source has been very much helpful in doing our research. Read more about for loop java array and let us know what you think.
Frequently Asked Questions
What is for loop in Java with example?
The for loop can be used to loop through a set of numbers or letters. It can also be used to loop through a string and repeat a certain block of code. The for loop can be used to accomplish all of these things. In the most basic example, you can create a loop that repeats the print “Hello World” ten times. The for loop is a type of control structure in Java. It is used to perform a block of code multiple times. For loop in java is very easy to implement. Here is one example: int i; for (i = 0; i < 10; i++) { /*code to be performed*/ } Output: 0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9
How do you do a for loop in Java?
A ” for loop ” is a programming language statement that allows you to perform a set of instructions a certain number of times. The number of times the loop is repeated is called the loop counter or loop variable. The format of a for loop in java is: for(int loopCounter = 0; loopCounter < 5; loopCounter++) { cout << "This is number " < There are three types of loops in Java programming language. They are as follows: 1. While loop: (for a while) This is a loop that executes till the given condition is true. The condition is checked before the loop starts to execute. 2. Do-While Loop: (Do it while) This is a loop that executes at least once. This is the opposite of the while loop. The condition is checked after the loop starts to execute. 3. For Loop: (For a given number of times) This is a loop that executes a specific number of times. Loops are considered as one of the most important programming concept in Java Programming language. It is not just about the syntax for writing loops but also about the concept behind the looping. A loop can be broadly classified as either a sequence or a selection. A sequence is a set of instructions or a code that has to be executed repeatedly, whereas a selection is a set of instructions or a code that has to be executed on the condition that a certain condition is met. java loop programs for practiceenhanced for loop in java examplejava while loop examplefor loop java arrayfor-each loop in javajava loop programs examples pdf,People also search for,Feedback,Privacy settings,How Search works,While loop,Do while loop,For loop,Nested loop join,See more,java loop programs for practice,enhanced for loop in java example,java while loop example,for loop java array,for-each loop in java,java loop programs examples pdf,for loop example,nested for loop in javaWhat are the 3 types of loops in Java?
Related Tags: