in , ,

Java while and do…while Loop

Java while and do…while Loop
Java while and do…while Loop

Java while and do…while Loop

In this tutorial, we will learn how to use while and do while loop in Java with the help of examples.

In computer programming, loops are used to rehash a block of code. For instance, if you need to show a message multiple times, at that point you can use a loop. It’s only a straightforward model; you can accomplish substantially more with loops.

In the previous tutorial, you learned about Java for loop. Here, you are going to learn about while and do…while loops.


Java while loop

Java while loop is used to run a particular code until a specific condition is met. The syntax of the while loop is:

while (testExpression) {
    // body of loop
}

Here,

  • A while loop evaluates the textExpression inside the parenthesis ().
  • If the textExpression evaluates to true, the code inside the while loop is executed.
  • The textExpression is evaluated again.
  • This process continues until the textExpression is false.
  • When the textExpression evaluates to false, the loop stops.

To learn more about the conditions, visit Java relational and logical operators.


Flowchart of while loop


Example 1: Display Numbers from 1 to 5

// Program to display numbers from 1 to 5

class Main {
  public static void main(String[] args) {

    // declare variables
    int i = 1, n = 5;

    // while loop from 1 to 5
    while(i <= n) {
      System.out.println(i);
      i++;
    }
  }
}

Output

1
2
3
4
5

Here is how this program works.

IterationVariableCondition: i <= nAction
1sti = 1
n = 5
true1 is printed.
i is increased to 2.
2ndi = 2
n = 5
true2 is printed.
i is increased to 3.
3rdi = 3
n = 5
true3 is printed.
i is increased to 4.
4thi = 4
n = 5
true4 is printed.
i is increased to 5.
5thi = 5
n = 5
true5 is printed.
i is increased to 6.
6thi = 6
n = 5
falseThe loop is terminated

Example 2: Sum of Positive Numbers Only

// Java program to find the sum of positive numbers
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
      
    int sum = 0;

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // take integer input from the user
    System.out.println("Enter a number");
    int number = input.nextInt();
	   
    // while loop continues 
    // until entered number is positive
    while (number >= 0) {
      // add only positive numbers
      sum += number;

      System.out.println("Enter a number");
      number = input.nextInt();
    }
	   
    System.out.println("Sum = " + sum);
    input.close();
  }
}

Output

Enter a number
25
Enter a number
9
Enter a number
5
Enter a number
-3
Sum = 39

In the above program, we have used the Scanner class to take input from the user. Here, nextInt() takes integer input from the user.

The while loop proceeds until the user enters a negative number. During every emphasis, the number entered by the user is added to the sum variable.

At the point when the user enters a negative number, the loop ends. At long last, the total sum is shown.


Java do…while loop

The do…while loop is like while loop. In any case, the body of do…while loop is executed once before the test expression is checked. For instance,

do {
    // body of loop
} while(textExpression)

Here,

  • The body of the loop is executed at first. Then the textExpression is evaluated.
  • If the textExpression evaluates to true, the body of the loop inside the do statement is executed again.
  • The textExpression is evaluated once again.
  • If the textExpression evaluates to true, the body of the loop inside the do statement is executed again.
  • This process continues until the textExpression evaluates to false. Then the loop stops.

Flowchart of do…while loop


Let’s see the working of do…while loop.

Example 3: Display Numbers from 1 to 5

// Java Program to display numbers from 1 to 5

import java.util.Scanner;

// Program to find the sum of natural numbers from 1 to 100.

class Main {
  public static void main(String[] args) {

    int i = 1, n = 5;

    // do...while loop from 1 to 5
    do {
      System.out.println(i);
      i++;
    } while(i <= n);
  }
}

Output

1
2
3
4
5

Here is how this program works.

IterationVariableCondition: i <= nAction
 i = 1
n = 5
not checked1 is printed.
i is increased to 2.
1sti = 2
n = 5
true2 is printed.
i is increased to 3.
2ndi = 3
n = 5
true3 is printed.
i is increased to 4.
3rdi = 4
n = 5
true4 is printed.
i is increased to 5.
4thi = 5
n = 5
true6 is printed.
i is increased to 6.
5thi = 6
n = 5
falseThe loop is terminated

Example 4: Sum of Positive Numbers

// Java program to find the sum of positive numbers
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
      
    int sum = 0;
    int number = 0;

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);
	   
    // do...while loop continues 
    // until entered number is positive
    do {
      // add only positive numbers
      sum += number;
      System.out.println("Enter a number");
      number = input.nextInt();
    } while(number >= 0); 
	   
    System.out.println("Sum = " + sum);
    input.close();
  }
}

Output

Enter a number
25
Enter a number
9
Enter a number
5
Enter a number
-3
Sum = 39

Here, the user enters a positive number, that number is added to the sum variable. Furthermore, this cycle proceeds until the number is negative. At the point when the number is negative, the loop ends and shows the whole without including the negative number.

Output

Enter a number
-8
Sum is 0

Here, the user enters a negative number. The test condition will be false but the code within the loop executes once.


Infinite while loop

If the condition of a loop is always true, the loop runs for infinite times (until the memory is full). For example,

// infinite while loop
while(true){
    // body of loop
}

Here is an example of an infinite do…while loop.

// infinite do...while loop
int count = 1;
do {
   // body of loop
} while(count == 1)

In the above programs, the textExpression is always true. Hence, the loop body will run for infinite times.


for and while loops

The for loop is used when the number of iterations is known. For example,

for (let i = 1; i <=5; ++i) {
   // body of loop
}

And while and do…while loops are generally used when the number of iterations is unknown. For example,

while (condition) {
    // body of loop
}

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How to Create a Transparent Gradient

How to Create a Transparent Gradient in CorelDRAW

How to Add Lens Flare in CorelDRAW

How to Add Lens Flare Effect in CorelDRAW