How to Make a Java Program Ask for Input Again
In computer programming, loops are used to repeat a block of code. For instance, if you want to show a bulletin 100 times, then y'all tin use a loop. Information technology'southward only a unproblematic instance; you can achieve much more with loops.
In the previous tutorial, yous learned about Java for loop. Here, y'all 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,
- A
while
loop evaluates the textExpression within the parenthesis()
. - If the textExpression evaluates to
true
, the lawmaking inside thewhile
loop is executed. - The textExpression is evaluated over again.
- This procedure continues until the textExpression is
imitation
. - When the textExpression evaluates to
fake
, the loop stops.
To learn more about the weather, visit Java relational and logical operators.
Flowchart of while loop
Instance 1: Display Numbers from 1 to five
// Program to brandish numbers from 1 to 5 form Principal { public static void main(Cord[] args) { // declare variables int i = 1, n = 5; // while loop from 1 to five while(i <= n) { Organisation.out.println(i); i++; } } }
Output
1 two 3 four v
Hither is how this programme works.
Iteration | Variable | Status: i <= n | Action |
---|---|---|---|
1st | i = ane northward = v | true | 1 is printed. i is increased to 2. |
2nd | i = 2 n = 5 | truthful | 2 is printed. i is increased to 3. |
3rd | i = three n = 5 | truthful | 3 is printed. i is increased to iv. |
4th | i = iv northward = 5 | true | 4 is printed. i is increased to 5. |
5th | i = 5 n = five | true | v is printed. i is increased to half dozen. |
sixth | i = 6 due north = v | fake | The loop is terminated |
Example two: Sum of Positive Numbers Merely
// Java program to find the sum of positive numbers import java.util.Scanner; class Chief { public static void master(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 simply positive numbers sum += number; Arrangement.out.println("Enter a number"); number = input.nextInt(); } System.out.println("Sum = " + sum); input.shut(); } }
Output
Enter a number 25 Enter a number 9 Enter a number v Enter a number -3 Sum = 39
In the higher up plan, 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 past 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 practise...while
loop is similar to while loop. Yet, the body of practise...while
loop is executed once earlier the test expression is checked. For example,
do { // body of loop } while(textExpression);
Here,
- The trunk of the loop is executed at get-go. And then the textExpression is evaluated.
- If the textExpression evaluates to
true
, the body of the loop inside thepractice
statement is executed again. - The textExpression is evaluated once more.
- If the textExpression evaluates to
true
, the body of the loop inside thedo
argument is executed once more. - This procedure continues until the textExpression evaluates to
imitation
. So the loop stops.
Flowchart of exercise...while loop
Let's meet the working of exercise...while
loop.
Example three: 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 = ane, n = 5; // practice...while loop from 1 to v do { Organization.out.println(i); i++; } while(i <= northward); } }
Output
1 ii iii 4 5
Here is how this programme works.
Iteration | Variable | Condition: i <= n | Activeness |
---|---|---|---|
i = i north = 5 | not checked | one is printed. i is increased to two. | |
1st | i = 2 n = 5 | true | 2 is printed. i is increased to 3. |
2nd | i = 3 n = 5 | true | 3 is printed. i is increased to 4. |
3rd | i = four north = 5 | truthful | 4 is printed. i is increased to v. |
4th | i = 5 n = five | truthful | 6 is printed. i is increased to vi. |
5th | i = 6 n = 5 | false | The loop is terminated |
Instance 4: Sum of Positive Numbers
// Java program to find the sum of positive numbers import java.util.Scanner; grade Main { public static void principal(String[] args) { int sum = 0; int number = 0; // create an object of Scanner form Scanner input = new Scanner(System.in); // practice...while loop continues // until entered number is positive practice { // add only positive numbers sum += number; System.out.println("Enter a number"); number = input.nextInt(); } while(number >= 0); System.out.println("Sum = " + sum); input.shut(); } }
Output one
Enter a number 25 Enter a number 9 Enter a number five 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 -viii Sum is 0
Here, the user enters a negative number. The exam condition will exist false
but the code inside of the loop executes once.
Infinite while loop
If the condition of a loop is always true
, the loop runs for infinite times (until the retention is full). For case,
// space while loop while(true){ // body of loop }
Here is an example of an space do...while
loop.
// infinite practice...while loop int count = 1; practise { // trunk of loop } while(count == 1)
In the above programs, the textExpression is always true
. Hence, the loop torso will run for infinite times.
for and while loops
The for
loop is used when the number of iterations is known. For case,
for (let i = i; 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 (status) { // body of loop }
Source: https://www.programiz.com/java-programming/do-while-loop
0 Response to "How to Make a Java Program Ask for Input Again"
Post a Comment