Do looping

Java do-while Loop

The Java do-while loop is used to iterate a portion of the program repeatedly until the specified requirement is true.

If the loop needs to be executed at least once and the number of iterations is uncertain, a do-while loop is recommended.
An exit control loop is a type of do-while loop in Java. Therefore, in contrast to the loop and for the loop, the do-while checks the status at the end of the loop body.
The Java do-while loop is executed at least once because the condition is checked after the loop body.

Syntax of do-while loop:

Java do while loop syntax is as follows: 

do

{

  statement(s);

} while(condition);

 The expression for the do-while loop must return a boolean value, otherwise, it will throw compile-time error. 

Here’s a more detailed explanation of each part of the “do-while” loop:

  1. do: This keyword marks the beginning of the “do-while” loop. It is used to indicate that the block of code following it should be executed at least once, regardless of the condition.
  2. {statement}: This represents the body of the loop, which contains the code that will be executed repeatedly as long as the condition is true.
  3. While  (condition): This is the loop continuation condition. The loop will continue as long as this condition evaluates to true. After each iteration of the loop body, the condition is checked again. If it is still true, the loop will execute the body again. If the condition becomes false, the loop will terminate, and the program will continue with the code after the loop.

The “do-while” loop can be handy when you want to execute a block of code at least once, and then you want to repeat it based on a certain condition.

 Example:

public class LoopExample {

    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println(i);
            i++;
        } while (i < 5);

    }

}

Output:

0
1
2
3
4

Explanation:

  1. int i = 0; Here, you are declaring a variable i and initializing it with the value 0.
  2. do { … } while (i < 5);This is a “do-while” loop. The code within the block will be executed repeatedly as long as the condition i < 5 is true.
  3. System.out.println(i); This line of code prints the current value of i to the console.
  4. i++;This line increments the value of i by 1 in each iteration of the loop. It’s a shorthand for i = i + 1.

Now, let’s walk through the iterations of the loop:

The given do-while loop iterates through a sequence of numbers as follows:

Iteration 1:

  • i is 0.
  • Print the value of i (which is 0).
  • Increment i by 1.

Iteration 2:

  • i is 1.
  • Print the value of i (which is 1).
  • Increment i by 1.

Iteration 3:

  • i is 2.
  • Print the value of i (which is 2).
  • Increment i by 1.

Iteration 4:

  • i is 3.
  • Print the value of i (which is 3).
  • Increment i by 1.

Iteration 5:

  • i is 4.
  • Print the value of i (which is 4).
  • Increment i by 1.

Iteration 6:

  • i is 5.
  • The loop terminates because the condition i < 5 is no longer satisfied.

The loop runs a total of 6 iterations, printing the numbers 0 through 4 in sequential order. It stops after the sixth iteration because the value of i becomes 5, which makes the condition i < 5 false, causing the loop to exit.

Another Example of the do-while loop :

In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and Decrement the variable used in the condition . Otherwise, the loop will execute infinitely.

public class LoopExample {

    public static void main(String[] args) {
        int i = 10;
        do {
            System.out.println(i);
            i--;
        } while (i > 1);
    }

}

Output:

10
9
8
7
6
5
4
3
2

Explanation:

  1. int i = 10; Here, you are declaring a variable i and initializing it with the value 10.
  2. do { … } while (i > 1); This is a “do-while” loop. The code within the block will be executed repeatedly if the condition i > 1 is true.
  3. System.out.println(i); This line of code prints the current value of i to the console.
  4. i–; This line decrements the value of i by 1 in each iteration of the loop. It’s a shorthand for i = i – 1.

Now, let’s walk through the iterations of the loop:

  1. The provided do-while loop iterates through a sequence of numbers as follows:

    Iteration 1:

    • i is 10.
    • Print the value of i (which is 10).
    • Decrement i by 1.

    Iteration 2:

    • i is 9.
    • Print the value of i (which is 9).
    • Decrement i by 1.

    Iteration 3:

    • i is 8.
    • Print the value of i (which is 8).
    • Decrement i by 1.

    Iteration 4:

    • i is 7.
    • Print the value of i (which is 7).
    • Decrement i by 1.

    Iteration 5:

    • i is 6.
    • Print the value of i (which is 6).
    • Decrement i by 1.

    Iteration 6:

    • i is 5.
    • Print the value of i (which is 5).
    • Decrement i by 1.

    Iteration 7:

    • i is 4.
    • Print the value of i (which is 4).
    • Decrement i by 1.

    Iteration 8:

    • i is 3.
    • Print the value of i (which is 3).
    • Decrement i by 1.

    Iteration 9:

    • i is 2.
    • The loop terminates because the condition i > 1 is no longer satisfied.

    The loop runs a total of 9 iterations, printing the numbers 10 through 2 in sequential order. It stops after the ninth iteration because the value of i becomes 2, which makes the condition i > 1 false, causing the loop to exit

Iterating array using do-while loop

Here we have an integer array and we are iterating the array and displaying each element using a do-while loop.

Example:

public class doWhileLoopingExample  {
    public static void main(String[] args) {
        int arr[] = { 3, 21, 35, 6 };

     // i starts with 0 as array index starts with 0
        int i = 0;
        do {
            System.out.println(arr[i]);

         i++;
        } while (i < 4);

Output:

3
21
35
6

Explanation:

  1. int arr[] = { 3, 21, 35, 6 }; This line declares and initializes an array of integers with four elements: {3, 21, 35, 6}.
  2. int i = 0; This line initializes an integer variable i with the value 0. i will be used to access elements in the array arr.
  3. do { … } while (i < 4); This is a “do-while” loop that will execute the code within the block at least once, and then it will continue as long as the condition i < 4 is true.
  4. System.out.println(arr[i]); This line prints the element at index i in the array arr to the console. In the first iteration, i is 0, so it prints arr[0], which is 3. In the second iteration, i becomes 1, so it prints arr[1], which is 21, and so on.
  5. i++; This line increments the value of i by 1 after each iteration. This is necessary to move to the next element in the array for the next iteration.

Now, let’s walk through the iterations of the loop:

  1. Iteration 1 (i = 0): The loop body is executed. The value of i is 0, so it prints arr[0], which is 3. Then, i is incremented by 1 (i becomes 1).
  2. Iteration 2 (i = 1): The loop body is executed again. The value of i is 1, so it prints arr[1], which is 21. Then, i is incremented by 1 (i becomes 2).
  3. Iteration 3 (i = 2): The loop body is executed again. The value of i is 2, so it prints arr[2], which is 35. Then, i is incremented by 1 (i becomes 3).
  4. Iteration 4 (i = 3): The loop body is executed again. The value of i is 3, so it prints arr[3], which is 6. Then, i is incremented by 1 (i becomes 4).
  5. Iteration 5 (i = 4): The loop continues with the condition i < 4. However, i is now equal to 4, and the condition 4 < 4 is false. So, the loop terminates.

 

 

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close