10 Loops in Java
10 Loops in Java
The Java for loop is used to iterate a part of the program several times. If the number
of iteration is fixed, it is recommended to use for loop.
1. Initialization: It is the initial condition which is executed once when the loop starts.
Here, we can initialize the variable, or we can use an already initialized variable. It is an
optional condition.
2. Condition: It is the second condition which is executed each time to test the condition
of the loop. It continues execution until the condition is false. It must return boolean
value either true or false. It is an optional condition.
3. Increment/Decrement: It increments or decrements the variable value. It is an
optional condition.
4. Statement: The statement of the loop is executed each time until the second condition
is false.
Syntax:
6.2M
108
Flowchart:
Example:
ForExample.java
1
2
3
4
5
6
7
8
9
10
Example:
NestedForExample.java
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Pyramid Example 1:
PyramidExample.java
Output:
*
* *
* * *
* * * *
* * * * *
Pyramid Example 2:
PyramidExample2.java
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
It works on the basis of elements and not the index. It returns element one by one in
the defined variable.
Syntax:
Example:
ForEachExample.java
Output:
12
23
44
56
78
Java Labeled For Loop
We can have a name of each Java for loop. To do so, we use label before the for loop.
It is useful while using the nested for loop as we can break/continue specific for loop.
Note: The break and continue keywords breaks or continues the innermost for loop
respectively.
Syntax:
1. labelname:
2. for(initialization; condition; increment/decrement){
3. //code to be executed
4. }
Example:
LabeledForExample.java
Output:
1 1
1 2
1 3
2 1
If you use break bb;, it will break inner loop only which is the default behaviour of any
loop.
LabeledForExample2.java
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
Syntax:
1. for(;;){
2. //code to be executed
3. }
Example:
ForExample.java
1. //Java program to demonstrate the use of infinite for loop
2. //which prints an statement
3. public class ForExample {
4. public static void main(String[] args) {
5. //Using no condition in for loop
6. for(;;){
7. System.out.println("infinitive loop");
8. }
9. }
10. }
Output:
infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c
Introduction The Java for loop is a control The Java while loop is a The Java do while loop is a
flow statement that iterates a control flow statement control flow statement that
part of the programs multiple that executes a part of executes a part of the
times. the programs repeatedly programs at least once and
on the basis of given the further execution
boolean condition. depends upon the given
boolean condition.
When to use If the number of iteration is If the number of iteration If the number of iteration is
fixed, it is recommended to is not fixed, it is not fixed and you must
use for loop. recommended to use have to execute the loop at
while loop. least once, it is
recommended to use the
do-while loop.
Syntax for(init;condition;incr/decr){ while(condition){ do{
// code to be executed //code to be executed //code to be executed
} } }while(condition);