Java While Loop Explaination

This chapter will examine the Java while loop with examples, hoping that it will help us in our learning process.

In Java, a while loop is a control flow instruction that repeatedly executes code when a given Boolean condition is met. As a repeating if statement, a while loop can be viewed as a conditional statement.

While loops are described as repeating if statements. The while loop should be implemented if the number of iterations is not fixed.

Java while loop can utilized when a block of statements needs to be executed repeatedly.

Loops

As long as a specified condition is met, a loop can execute a block of code.

It is convenient to use loops because they save time, reduce errors, and improve the readability of code.



Java While Loop

A while loop iterates through a block of code until a condition is met:

Syntax:

while (condition) { // Executable block of code }

Following is an example of a loop that runs as long as the variable (mrx) is less than 7:

Example: 

public class Main { public static void main(String[] args) { int mrx=0; while(mrx<7){ System.out.println("value: "+mrx); mrx++; } } }

Example: 

public class Main { public static void main(String[] args) {int ample=1;while(ample <= 10){ System.out.println(ample); ample++; } } }

 
Remember : It is important to increase the variable used in the condition otherwise the loop will never end!

Java-while-loop-result

What does a While loop do?

  1. The control is taken over by the while loop.

  2. Condition is reached in the flow
  3. The condition is tested.

    • The flow enters the body if Condition is true.

    • The flow exits the loop if Condition yields false

  4. The loop’s body executes the statements inside it.

  5. An update is performed.

  6. The control flows back to Step 2.

  7. Flow has exited the while loop.


The Do/While Loop

There is a variant of the while loop called the do/while loop. Once the code block is executed, it will check if the condition is true, and then repeat the loop if it is.

Syntax:

do { // Execution of code block
} while (condition);

Below is an example of a do/while loop. Even if the condition is false, the loop will still be executed at least once, since the code block is executed before the condition is tested.

Example: 

public class Main { public static void main(String[] args) { int mrx = 1; do { System.out.println(mrx); mrx++; } while (mrx <= 10); // The first ten digits will be printed } }

We can also use the do-while loop to print starting 10 numbers in a reverse order:

Example: 

public class Main { public static void main(String[] args) { int ample = 10; do { System.out.println(ample); ample–; } while (ample >= 1); // The first ten digits will be printed but in the reverse order } }

Important : Remember to increase the variable used in the condition, otherwise the loop will never end!

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *