How to Incorporate While Loop for Would You Like to Do It Again Java

In figurer programming, loops are used to repeat a block of lawmaking. For instance, if you want to show a message 100 times, so y'all tin utilise a loop. It's just a unproblematic instance; you lot tin achieve much more with loops.

In the previous tutorial, you learned about Java for loop. Here, yous are going to learn about while and exercise...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 within the parenthesis ().
  2. If the textExpression evaluates to true, the code inside the while loop is executed.
  3. The textExpression is evaluated over again.
  4. This process continues until the textExpression is simulated.
  5. When the textExpression evaluates to simulated, the loop stops.

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


Flowchart of while loop

Flowchart of while loop in Java
Flowchart of Java while loop

Example one: Display Numbers from 1 to 5

          // Program to brandish numbers from ane to 5  form Main {   public static void main(String[] args) {      // declare variables     int i = 1, n = five;      // while loop from 1 to 5     while(i <= due north) {       Organisation.out.println(i);       i++;     }   } }        

Output

          i 2 3 4 5        

Hither is how this program works.

Iteration Variable Condition: i <= northward Activeness
1st i = one
due north = 5
true 1 is printed.
i is increased to two.
second i = 2
northward = 5
true 2 is printed.
i is increased to iii.
3rd i = 3
north = v
true 3 is printed.
i is increased to 4.
4th i = 4
northward = five
truthful four is printed.
i is increased to 5.
5th i = 5
northward = v
true five is printed.
i is increased to 6.
6th i = half-dozen
n = 5
faux The loop is terminated

Example 2: Sum of Positive Numbers Merely

          // 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 grade     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;        Organization.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 -three Sum = 39        

In the higher up program, we have used the Scanner class to take input from the user. Hither, 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 full sum is displayed.


Java do...while loop

The exercise...while loop is like to while loop. However, the trunk of exercise...while loop is executed once before the test expression is checked. For example,

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

Here,

  1. The torso of the loop is executed at kickoff. So the textExpression is evaluated.
  2. If the textExpression evaluates to true, the body of the loop inside the practice statement is executed over again.
  3. The textExpression is evaluated once again.
  4. If the textExpression evaluates to truthful, the body of the loop inside the exercise statement is executed again.
  5. This procedure continues until the textExpression evaluates to fake. Then the loop stops.

Flowchart of practice...while loop

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

Let'due south see the working of do...while loop.

Example three: Display Numbers from 1 to 5

          // Java Program to display numbers from i to v  import java.util.Scanner;  // Program to notice the sum of natural numbers from i to 100.  form Principal {   public static void primary(Cord[] args) {      int i = 1, n = five;      // do...while loop from ane to five     do {       Organization.out.println(i);       i++;     } while(i <= n);   } }        

Output

          1 2 3 4 5        

Hither is how this plan works.

Iteration Variable Condition: i <= n Action
i = i
northward = 5
not checked one is printed.
i is increased to two.
1st i = two
n = v
truthful ii is printed.
i is increased to 3.
2nd i = 3
n = five
true 3 is printed.
i is increased to iv.
3rd i = 4
n = 5
truthful iv is printed.
i is increased to 5.
quaternary i = 5
n = five
true half-dozen is printed.
i is increased to 6.
fifth i = six
n = five
false The loop is terminated

Example 4: Sum of Positive Numbers

          // Coffee program to detect the sum of positive numbers import java.util.Scanner;  form Chief {   public static void primary(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     exercise {       // add but positive numbers       sum += number;       System.out.println("Enter a number");       number = input.nextInt();     } while(number >= 0);  	        System.out.println("Sum = " + sum);     input.close();   } }        

Output 1

          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. And this process continues until the number is negative. When the number is negative, the loop terminates and displays the sum without adding the negative number.

Output 2

          Enter a number -eight Sum is 0        

Here, the user enters a negative number. The exam status will be false just the lawmaking inside of the loop executes in one case.


Infinite while loop

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

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

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

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

In the in a higher place programs, the textExpression is always true. Hence, the loop body will run for space times.


for and while loops

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

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

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

          while (condition) {     // torso of loop }        

rischlivalwas.blogspot.com

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

0 Response to "How to Incorporate While Loop for Would You Like to Do It Again Java"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel