Perulangan (Looping) pada Java
For Loop
for(initialization; condition; increment/decrement){
//statement or code to be executed
}
//statement or code to be executed
}
> for(int i=1;i<=5;i++){
> System.out.print(i+" ");
> }
> System.out.print(i+" ");
> }
output
1 2 3 4 5
1 2 3 4 5
While Loop
while (boolean condition){
//statement or code to be executed
}
//statement or code to be executed
}
> int x = 1;
>
>
while
(x <=
5
){
>
System.out.print(
x+" ");
>
x++;
>
}
output
1 2 3 4 5
1 2 3 4 5
Do While Loop
do{
//statement or code to be executed
}while (condition);
//statement or code to be executed
}while (condition);
> int x = 1;
> do{
> System.out.print(x+" ");
> x++;
> }while (x <= 5);
> do{
> System.out.print(x+" ");
> x++;
> }while (x <= 5);
output
1 2 3 4 5
1 2 3 4 5