xxxxxxxxxx
//while loop
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
xxxxxxxxxx
do {
//something you want to execute at least once
} while (someBooleanCondition);
xxxxxxxxxx
int count = 10;
while(count < 10) {
System.out.println(count);
count++;
}
//while will run when if the condition is true
//if count is less then 10 the condition is true
//if count is more then 10 or equals to 10 it is false.
xxxxxxxxxx
while(i < 5) //while i < 5 stay in the loop
{
System.out.print(i);
i++;
}
/*
do someting
change variable
call methods
etc...
*/
xxxxxxxxxx
// Starting on 0:
int i=0; // initialization
while(i<10){ // condition
System.out.println(i);
i++; // increment operation
}
// Starting on 1:
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
xxxxxxxxxx
//do-while loop
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
xxxxxxxxxx
public class LoopExample {
public static void main(String[] args) {
// using a for loop
System.out.println("Using a for loop:");
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
// using a while loop
System.out.println("\nUsing a while loop:");
int j = 1;
while (j <= 5) {
System.out.println("Iteration " + j);
j++;
}
}
}
xxxxxxxxxx
int ctr = 1;
do
{
System.out.println(ctr); // print ctr value
ctr++; // increment ctr value by 1
}while(ctr <= 10);