For loop in Java programming language
For loop in Java programming language
In this tutorial, we will discuss For loop in Java programming language
In the Java language, there are three types of basic looping statements
for loop
while loop
do while loop
for loop
In the Java language, for loop is used to evaluate and execute Java code repeatedly until the test expression is false. The for loop is executed when the test expression is evaluated and the condition is satisfied.
for loop consist of three statements which typically use:
- initialization statements
- Test expression – Boolean condition(result may be true or false)
- Updating statements(increment or decrement statements)
Declaration
The syntax of the for loop
for(initialization; test_expression; updating statements)
{
statement(s)
//here we can put suitable statements
}
Example of for loop
for(int x=0; x<=10; x++) { System.out.println(x); }
Explanation of for loop
Output
0 to 10
//parenthesis () – used to control the order of operations in an expression
//Curly braces {} – Braces are used to group the statements in the loop
Initiation –
First, it always begins with the initialization statement. The initialization statement is executed only once. Initialization is the entry point to the for loop. This allows to declare and initialize any control variable ending with a semicolon(;).
condition –
Next, the for loop moves to the conditional statement for test condition. The condition is evaluated, if it is true, the statements in the body part of for loop are executed but, if the condition is the false body of the loop does not execute.
updating the statement
Finally, the loop goes to updating statements section and it allows you to update any loop control variables until the condition becomes false. This statement can be updated until a semicolon appears after the condition.
For loop flow diagram
The loop begins the initialization parts.
Then the test expression is evaluated.
When the Boolean expression is true, the loop executes and updates statements.
Finally, when the Boolean expression is false, the loop exits from the loop.
Various form of for loop in Java
form 1
for(int i=0; i<=10; i++){ //statement(s); }
form 2
int i; for(i=0; i<=10; i++){ //statement(s) }
form 3
int i=0; for(; i<=10; i++){ //statement(s) }
form 4
int i=0; for(; i<=10;){ //statement(s) i++ }
Example
Program 1
class for_java{ public static void main(String args[]){ for(int x=0; x<=10; x++){ System.out.println(x); } } }
When the above code is executed, it produces the following results:
0 1 2 3 4 5 6 7 8 9 10
In the above program, print 1 to 10 natural number
Program 2
//find the total of 1 to 100 using for loop class for_java1{ public static void main(String args[]){ int sum=0;// variable declaration for(int i=0; i<=100; i++){ sum=sum+=i; //equal to sum=sum+i } System.out.println("Total value of sum of 1 to 100 is:"+sum); } }
When the above code is executed, it produces the following results:
Total value of sum of 1 to 100 is : 5050
find factorial using for loop in Java
class FindFactForJava{ public static void main(String args[]){ int num=5;//find factorial for 5 numbers int fact =1; for(int i=1; i<=num; i++){ fact=fact*i; } System.out.println("Factorial of "+num+" is :"+fact ); } }
When the above code is executed, it produces the following results:
Factorial of 5 is : 120
Display elements of the array using for loop in java
class ArrayForLoop{ public static void main(String args[]){ int arr[]={56,76,78,98,73,46};//declaration and initialization //printing elements of array for(int i=0; i<=arr.length-1; i++){ System.out.println(arr[i]); } } }
In the above program, we can use to display single dimension array elemnet using for loop
When the above code is executed, it produces the following results:
56 76 78 98 73 46
Infinitive for loop
Program 1
//infinite for loop class Infinite_for{ public static void main(String args[]){ for(;;){ System.out.println("This is infinite for loop.."); } } }
When the above code is executed, it produces the following results:
This is infinite for loop.. This is infinite for loop.. This is infinite for loop.. This is infinite for loop.. This is infinite for loop.. This is infinite for loop.. This is infinite for loop.. This is infinite for loop.. ....... ....... control the flow - press ctrl+c
Program 2
//infinite for loop class Infinite_for1{ public static void main(String args[]){ for(int x=0; x>=0; x++){ System.out.println("x value is :"+x); } } }
When the above code is executed, it produces the following results:
x value is :1 x value is :2 x value is :3 x value is :4 x value is :5 x value is :6 ..... ..... ..... To control the flow - ctrl+c
Program 3
//infinite for loop class Infinite_for2{ public static void main(String args[]){ for(;true;){ System.out.println("this execution never end.."); } } }
When the above code is executed, it produces the following results:
this execution never end.. this execution never end.. this execution never end.. this execution never end.. this execution never end.. this execution never end.. this execution never end.. this execution never end.. ............... ............... To control the flow - ctrl+c
There are other Java language keywords that are similar to this post
Suggested for you
While loop in C language
For loop in C++ language
While loop in C++ language
For loop in Python language
While loop in Python language