Java for loop
In this chapter you will learn:
- What is Java for loop
- How to write for loop
- Example - A simple for loop
- Example - loops reversively
- Example - Calculate the prime number with for loop
- Example - How to use comma in the for loop statement
- How to write different forms of for loop
- How to write nested for loops
Description
Java for
loop statement provides a powerful way of writing loop statement.
Syntax
The simplest form of the for
loop is shown here:
for(initialization; condition; iteration)
statement;
Java for loop statement has three parts:
initialization
sets a loop control variable to an initial value.condition
is aBoolean
expression that tests the loop control variable. Ifcondition
istrue
, the for loop continues to iterate. Ifcondition
isfalse
, the loop terminates.- The
iteration
determines how the loop control variable is changed each time the loop iterates.
Example
Here is a short program that illustrates the for loop.
i
is the loop control variable and i
is initialized to zero in the
initialization.
At the start of each iteration, the conditional test x < 10
is performed.
If the outcome of this test is true, the println()
statement is executed,
and then the iteration portion of the loop is executed.
This process continues until the conditional test is false
.
public class Main {
public static void main(String args[]) {
int i;/* ww w .ja v a 2 s . com*/
for (i = 0; i < 10; i = i + 1)
System.out.println("This is i: " + i);
}
}
This program generates the following output:
Example 2
The following code writes the code logic from above again but loops reversively:
public class Main {
public static void main(String args[]) {
for (int n = 10; n > 0; n--)
System.out.println("n:" + n);
}// w ww . j ava 2s. c o m
} ]]>
The output:
Example 3
Here is a program that tests for prime numbers using for
loop statement.
public class Main {
public static void main(String args[]) {
int num;/*from ww w .ja v a 2 s . c om*/
boolean isPrime = true;
num = 50;
for (int i = 2; i <= num / 2; i++) {
if ((num % i) == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
The output:
Example 4
Java allows two or more variables to control a for
loop.
And you can include multiple statements in both the initialization and iteration portions of
the for
loop.
Each statement is separated from the next by a comma.
Here is an example:
public class Main {
public static void main(String args[]) {
//from ww w. j av a 2s.com
for (int a = 1, b = 4; a < b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}
The program generates the following output:
Example 5
The three sections of the for
can be used for any purpose and parts of the
for
loop can be empty.
public class Main {
public static void main(String args[]) {
int i = 0;/* w ww . ja v a 2s . c o m*/
boolean done = false;
for (; !done;) {
System.out.println("i is " + i);
if (i == 10)
done = true;
i++;
}
}
}
The output:
Example 6
for
loop can be nested to produce powerful logic, for example,
we can use nested for
loop to iterate a two-dimensional array.
For example, here is a program that nests for
loops:
public class Main {
public static void main(String args[]) {
for (int i = 0; i < 10; i++) {
for (int j = i; j < 10; j++)
System.out.print(".");
System.out.println();//from w ww . java 2s . com
}
}
}
The output produced by this program is shown here:
Next chapter...
What you will learn in the next chapter:
- What is Java for each loop
- How to write for each loop
- Example - for each loop
- Example - for-each style loop to iterate a two-dimensional array
- Example - search an array element with for each loop