Java Tutorials
Java Methods
Java Classes
Java File Handling
Loops can block a code block as long as the specified condition is reached.
Loops work because they save time, reduce errors, and make the code more readable.
the while
loop entering the code block as long as the condition stated is true
:
while (condition) {
// code block to be executed
}
In the example below, the code in the loop will apply, repeatedly, as long as the variable (i) is less than 5:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Note: Do not forget to increase the flexibility used in the case, otherwise the loop will not expire!
Thedo / while
loop is a type of
do {
// code block to be executed
}
while (condition);
The example below uses a do/while
loop. The loop will always be used at least once, even if the condition is false, because the code cache is used before the status check:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
Do not forget to increase the flexibility used in the case, otherwise the loop will not expire!