Java - while Loop
Java while loop is a control flow statement and it will execute the block of statements as long as the
specified conditional expression is valid (i.e., true)
Example: #1 - Simple while Loop
Print the number from 1 to 5 using while loop
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 1;
while(counter <= 5) {
System.out.println(counter);
// Increment the number by 1 using Post Increment
counter++;
}
}
}
Example: #2 - while Loop (un-reached code)
Print the number from 1 to 5 in the console, but the starting number is greater than the requested logic. So the
while loop conditional expression gets failed and final result is that the
while loop block is un-reachable.
Print the number from 1 to 5 in the console. In below example we have set
the initial counter value as 10
which will not satisfy the condition of while loop while(counter <=5) in any situation.
So the while loop conditional expression will get fail and the code written
inside the while loop block will never execute (un-reachable code).
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 10;
System.out.println("Execution before while loop");
while(counter <= 5) {
System.out.println(counter);
// Increment the number by 1 using Post Increment
counter++;
}
System.out.println("Execution after while loop");
}
}
Java Output Screen
Execution before while loop
Execution after while loop
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3 - while Loop (Infinite loop)
Print the number form 1 to 5, the while loop has the valid conditional expression
but the code inside the while block doesn't have any statement to increment
the counter variable. So the conditional expression won't reach the termination
in otherwords it loops endlessly.
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 1;
while(counter <= 5) {
System.out.println(counter);
}
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Java while Loop with break Statement
The break statement is a termination statement, once the break
statement encountered in a while loop, immediately it will terminate the looping execution and
it return backs to the main statement execution.
Example: #4 - while Loop with break Statement
Terminate the while loop once the console printed the number 3 using the
break statement.
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 1;
while(counter <= 5) {
System.out.println(counter);
// Terminate the execution once counter reached 3
if(counter == 3) {
break;
}
// Increment the number by 1 using Post Increment
counter++;
}
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Java while Loop with continue Statement
The continue statement is a skipping statement of a current iteration, once the continue
statement encountered in a while loop, immediately it will skip the current iteration and
it will continue the rest of the operation till the conditional expression is true.
Example: #5 - while Loop with continue Statement
Skip the while loop once the counter has reached the value 3 using the
continue statement.
Java Input Screen
class ControlFlowStatement {
public static void main(String[] args) {
int counter = 0;
while(counter <= 5) {
// Increment the number by 1 using Post Increment
counter++;
// skips the iteration once counter reached 3
if(counter == 3) {
continue;
}
System.out.println(counter);
}
}
}
BBMINFO