In This Article, You Will Learn about Java While Loop.
Java Booleans – Before moving ahead, let’s know a bit about Java Switch.
Table of Contents
Loops
Loop executes to a block of code as long as a specified condition is met.
Loop is majorly used to reduce the lines of code, to make code easy to write, read & understand, and save time & eliminate errors.
Java While Loop
The while loop iterate through a block of code as long as a specified condition is true:
Syntax
while (condition) {
// code block to be executed
}
Example: Use the while loop to loop from the number 10 to 1.
public class Main {
public static void main(String[] args) {
int i = 10;
while (i > 0) {
System.out.println(i);
i--;
}
}
}
Note: Do not forget to decrease the value of the variable used in the condition; otherwise, the loop will never stop.
In the example above, the code in the loop will run, over and over again, as long as a variable (i) is 1.
Explanation –
i = 10,
Then, deduct the 1 from 10, which becomes 9.
Again,
i = 9,
Then, deduct the 1 from the 9, which becomes 8.
Again,
i = 8,
Then, deduct the 1 from the 8, which becomes 7, and so on….
Over and again, now the last step
i = 1,
Then, deduct the 1 from 1, which becomes 0,
Now i = 0, since i > 0 (0 > 0) is not true; therefore it breaks the loop and come out of the loop.
Example: Use the while loop to loop from the number 0 to 6.
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 6) {
System.out.println(i);
i++;
}
}
}
Note: Do not forget to increment the value of the variable used in the condition; otherwise, the loop will never stop.
In the example above, the code in the loop will run, over and over again, as long as a variable (i) is less than 6.
At starting, i = 0,
In the next step, condition is checked (0 < 6),
Since the condition is true; therefore, it executes the number 0,
Then, increment the 0 from 1, which becomes 1.
This procedure will continue until i = 6 ( 6 < 6 ) and the condition goes false, stopping the Loop.
Do/While Loop
Do/While is another type of Loop. This Loop will execute once before the condition is checked; then, it will repeat the Loop as long as it is true.
Syntax
do {
// code block to be executed
}
while (condition);
Example: Use the Do/While loop to loop from the number 10 to 1.
public class Main {
public static void main(String[] args) {
int i = 10;
do {
System.out.println(i);
i--;
}
while (i > 0);
}
}
Note: Do not forget to decrease the value of the variable used in the condition; otherwise, the loop will never stop.
The example above uses a do/while loop. The loop will consistently execute at least once, whether true or false because the code block is executed before the condition is tested.
Explanation –
At starting, i = 10,
In the next step, execute number 10
Then, deduct the 1 from 10, which becomes 9.
Now checked condition, since ( 9 > 0), it means the state is true; therefore, it repeats the same procedure repeatedly until i = 0, and the condition goes false, which stops the loop.
Example:
public class Main {
public static void main(String[] args) {
int k = 0;
do {
System.out.println(k);
k++;
}
while (k < 10);
}
}
Note: Do not forget to increase the value of the variable used in the condition; otherwise, the loop will never stop.
The example above uses a do/while loop. The loop will consistently execute at least once, whether true or false because the code block is executed before the condition is tested.
Explanation –
At starting, k = 0,
In the next step, execute the number 0
Then, increment the 1 to 0, which becomes 1.
Now checked condition, since ( 1 < 10) it means the state is true; therefore, it repeats the same procedure repeatedly until k = 10, and the condition goes false, which stops the loop.
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: