Loops in Java
Loops in Java
Syntax:
for(initialization; condition; increment/decrement){
//statement or code to be executed
}
------------------------------------------------------------For
Loop-------------------------------------------------------
//Java Program to demonstrate the example of for loop
//which prints table of 1
public class ForExample {
public static void main(String[] args) {
//Code of Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
Output :-
1
2
3
4
5
6
7
8
9
10
Output :-
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
-------------------------------------------------For-Each
Loop------------------------------------------
Syntax:
Output:
12
23
44
56
78
------------------------------------------Infinitive for
Loop--------------------------------------
Syntax:
for(;;){
//code to be executed
}
------------------------------------------------------While
Loop-------------------------------------
Syntax:
while (condition){
//code to be executed
I ncrement / decrement statement
}
output
1
2
3
4
5
6
7
8
9
10
while(true){
//code to be executed
}
--------------------------------------------do-while
Loop---------------------------------
Syntax:
do{
//code to be executed / loop body
//update statement
}while (condition);
OUTPUT:-
1
2
3
4
5
6
7
8
9
10
Syntax:
do{
//code to be executed
}while(true);