Ask User to Input Again Using Switch Statement Java
In calculator programming, loops are used to echo a block of lawmaking. For example, if yous want to bear witness a message 100 times, so you can apply a loop. It's but a uncomplicated example; you can reach much 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.
Coffee 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) { // trunk of loop } Here,
- A
whileloop evaluates the textExpression inside the parenthesis(). - If the textExpression evaluates to
truthful, the code inside thewhileloop is executed. - The textExpression is evaluated once again.
- This procedure 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
Case 1: Display Numbers from ane to 5
// Program to display numbers from i to v class Main { public static void main(String[] args) { // declare variables int i = i, n = five; // while loop from 1 to 5 while(i <= n) { Organization.out.println(i); i++; } } } Output
ane 2 3 4 5
Here is how this plan works.
| Iteration | Variable | Status: i <= n | Action |
|---|---|---|---|
| 1st | i = 1 due north = 5 | true | one is printed. i is increased to 2. |
| 2d | i = 2 n = v | true | 2 is printed. i is increased to iii. |
| tertiary | i = 3 n = 5 | true | 3 is printed. i is increased to four. |
| 4th | i = 4 due north = 5 | true | 4 is printed. i is increased to v. |
| fifth | i = v northward = 5 | true | v is printed. i is increased to six. |
| 6th | i = 6 n = five | false | The loop is terminated |
Example two: Sum of Positive Numbers Simply
// Java programme 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); // have 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.shut(); } } Output
Enter a number 25 Enter a number 9 Enter a number 5 Enter a number -3 Sum = 39
In the above program, we accept 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.
Coffee exercise...while loop
The practice...while loop is like to while loop. Even so, the body of practise...while loop is executed once before the test expression is checked. For example,
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 thedostatement is executed again. - The textExpression is evaluated once again.
- If the textExpression evaluates to
truthful, the body of the loop inside thedoargument is executed over again. - This process continues until the textExpression evaluates to
simulated. Then the loop stops.
Flowchart of do...while loop
Permit's see the working of do...while loop.
Example 3: Brandish Numbers from 1 to 5
// Java Programme to brandish numbers from 1 to 5 import coffee.util.Scanner; // Program to find the sum of natural numbers from one to 100. form Main { public static void main(Cord[] args) { int i = 1, n = 5; // do...while loop from ane to 5 do { Organisation.out.println(i); i++; } while(i <= n); } } Output
1 ii three 4 5
Hither is how this program works.
| Iteration | Variable | Condition: i <= n | Action |
|---|---|---|---|
i = 1 n = 5 | not checked | i is printed. i is increased to 2. | |
| 1st | i = 2 due north = 5 | true | two is printed. i is increased to three. |
| 2nd | i = 3 n = 5 | true | 3 is printed. i is increased to 4. |
| 3rd | i = 4 n = 5 | true | four is printed. i is increased to five. |
| fourth | i = v northward = v | true | 6 is printed. i is increased to six. |
| 5th | i = vi n = v | false | The loop is terminated |
Example 4: Sum of Positive Numbers
// Coffee program to find the sum of positive numbers import java.util.Scanner; class Principal { public static void chief(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 merely positive numbers sum += number; System.out.println("Enter a number"); number = input.nextInt(); } while(number >= 0); System.out.println("Sum = " + sum); input.shut(); } } Output 1
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 adding the negative number.
Output 2
Enter a number -8 Sum is 0
Here, the user enters a negative number. The test status volition be false only the code inside of the loop executes one time.
Infinite while loop
If the condition of a loop is always true, the loop runs for infinite times (until the retentivity is full). For instance,
// infinite while loop while(true){ // trunk of loop } Here is an case of an infinite do...while loop.
// infinite do...while loop int count = ane; do { // body of loop } while(count == i) In the above programs, the textExpression is e'er 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 example,
for (let i = ane; i <=5; ++i) { // torso of loop } And while and practise...while loops are generally used when the number of iterations is unknown. For example,
while (condition) { // body of loop } Source: https://www.programiz.com/java-programming/do-while-loop
0 Response to "Ask User to Input Again Using Switch Statement Java"
Post a Comment