Do While Loop to Read Input Again

In figurer programming, loops are used to repeat a cake of code. For case, if you want to testify a bulletin 100 times, then you can use a loop. It's just a simple case; you tin achieve much more with loops.

In the previous tutorial, you learned almost Java for loop. Hither, y'all are going to learn well-nigh while and do...while loops.


Java while loop

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

          while (testExpression) {     // body of loop }        

Here,

  1. A while loop evaluates the textExpression inside the parenthesis ().
  2. If the textExpression evaluates to true, the lawmaking within the while loop is executed.
  3. The textExpression is evaluated again.
  4. This process continues until the textExpression is imitation.
  5. When the textExpression evaluates to false, the loop stops.

To larn more than about the weather condition, visit Java relational and logical operators.


Flowchart of while loop

Flowchart of while loop in Java
Flowchart of Coffee while loop

Case 1: Display Numbers from 1 to five

          // Program to display numbers from 1 to 5  class Main {   public static void main(Cord[] args) {      // declare variables     int i = 1, northward = five;      // while loop from 1 to 5     while(i <= n) {       Arrangement.out.println(i);       i++;     }   } }        

Output

          i 2 3 4 5        

Hither is how this program works.

Iteration Variable Status: i <= n Action
1st i = 1
northward = 5
true 1 is printed.
i is increased to 2.
2d i = ii
n = v
true two is printed.
i is increased to 3.
3rd i = 3
northward = 5
truthful 3 is printed.
i is increased to iv.
4th i = iv
n = 5
true 4 is printed.
i is increased to 5.
5th i = 5
n = 5
true v is printed.
i is increased to 6.
6th i = 6
northward = 5
fake The loop is terminated

Example two: Sum of Positive Numbers Simply

          // Coffee program to discover the sum of positive numbers import java.util.Scanner;  grade Main {   public static void principal(Cord[] args) {            int sum = 0;      // create an object of Scanner course     Scanner input = new Scanner(Organization.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 together 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 ix Enter a number v 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 continues until the user enters a negative number. During each iteration, the number entered by the user is added to the sum variable.

When the user enters a negative number, the loop terminates. Finally, the total sum is displayed.


Java do...while loop

The practice...while loop is similar to while loop. However, the trunk of do...while loop is executed once before the test expression is checked. For example,

          practise {     // body of loop } while(textExpression);        

Here,

  1. The body of the loop is executed at start. And so the textExpression is evaluated.
  2. If the textExpression evaluates to true, the body of the loop inside the do statement is executed again.
  3. The textExpression is evaluated once once again.
  4. If the textExpression evaluates to true, the body of the loop within the do statement is executed again.
  5. This procedure continues until the textExpression evaluates to false. And so the loop stops.

Flowchart of exercise...while loop

Flowchart of do...while loop in Java
Flowchart of Java practice while loop

Let's meet the working of exercise...while loop.

Instance iii: Display Numbers from 1 to 5

          // Java Program to brandish numbers from 1 to 5  import java.util.Scanner;  // Programme to discover the sum of natural numbers from one to 100.  course Main {   public static void main(String[] args) {      int i = one, n = v;      // do...while loop from 1 to 5     practise {       System.out.println(i);       i++;     } while(i <= north);   } }        

Output

          one 2 3 iv 5        

Here is how this program works.

Iteration Variable Condition: i <= n Action
i = 1
n = 5
not checked ane is printed.
i is increased to 2.
1st i = 2
due north = 5
true 2 is printed.
i is increased to iii.
2d i = 3
n = 5
truthful 3 is printed.
i is increased to iv.
3rd i = 4
n = five
true four is printed.
i is increased to v.
4th i = v
n = 5
true half dozen is printed.
i is increased to 6.
5th i = 6
n = 5
imitation The loop is terminated

Example four: Sum of Positive Numbers

          // Java plan to notice 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 grade     Scanner input = new Scanner(Organisation.in); 	        // do...while loop continues      // until entered number is positive     do {       // add just positive numbers       sum += number;       Organization.out.println("Enter a number");       number = input.nextInt();     } while(number >= 0);  	        System.out.println("Sum = " + sum);     input.shut();   } }        

Output ane

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

Here, the user enters a positive number, that number is added to the sum variable. And this process continues until the number is negative. When the number is negative, the loop terminates and displays the sum without calculation the negative number.

Output 2

          Enter a number -8 Sum is 0        

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


Space while loop

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

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

Here is an instance of an infinite practice...while loop.

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

In the to a higher place 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 }        

tayloraland1957.blogspot.com

Source: https://www.programiz.com/java-programming/do-while-loop

0 Response to "Do While Loop to Read Input Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel