Would You Like to Run the Program Again Java Methods
In computer programming, loops are used to repeat a block of code. For example, if yous want to show a message 100 times, then you can use a loop. It's just a simple example; you can achieve much more than with loops.
In the previous tutorial, you learned about Coffee for loop. Hither, 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 sure 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 thewhile
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 virtually the weather, visit Java relational and logical operators.
Flowchart of while loop
Instance i: Display Numbers from 1 to v
// Program to display numbers from 1 to 5 form Master { public static void main(String[] args) { // declare variables int i = 1, n = 5; // while loop from 1 to v while(i <= n) { System.out.println(i); i++; } } }
Output
i 2 three four 5
Here is how this program works.
Iteration | Variable | Condition: i <= due north | Action |
---|---|---|---|
1st | i = i n = v | truthful | 1 is printed. i is increased to 2. |
2d | i = 2 n = 5 | true | 2 is printed. i is increased to iii. |
3rd | i = 3 n = 5 | true | 3 is printed. i is increased to 4. |
4th | i = 4 n = 5 | true | 4 is printed. i is increased to five. |
5th | i = 5 n = five | true | 5 is printed. i is increased to 6. |
6th | i = 6 n = 5 | false | The loop is terminated |
Example ii: 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(Arrangement.in); // accept 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 merely 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 ix Enter a number 5 Enter a number -3 Sum = 39
In the above program, we have used the Scanner grade 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 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. Still, the body of do...while
loop is executed once before the examination expression is checked. For case,
do { // body of loop } while(textExpression);
Hither,
- The body of the loop is executed at first. And so the textExpression is evaluated.
- If the textExpression evaluates to
truthful
, the body of the loop inside thedo
statement is executed again. - The textExpression is evaluated in one case once again.
- If the textExpression evaluates to
truthful
, the torso of the loop inside thedo
statement is executed again. - This procedure continues until the textExpression evaluates to
false
. And then the loop stops.
Flowchart of practise...while loop
Let'due south run into the working of do...while
loop.
Instance iii: Display Numbers from 1 to 5
// Java Program to brandish numbers from 1 to 5 import coffee.util.Scanner; // Program to find the sum of natural numbers from 1 to 100. form Main { public static void main(String[] args) { int i = 1, n = five; // do...while loop from i to 5 exercise { System.out.println(i); i++; } while(i <= north); } }
Output
1 2 3 four 5
Here is how this programme works.
Iteration | Variable | Condition: i <= n | Action |
---|---|---|---|
i = 1 n = 5 | not checked | 1 is printed. i is increased to 2. | |
1st | i = 2 n = 5 | truthful | 2 is printed. i is increased to three. |
2nd | i = 3 northward = 5 | true | 3 is printed. i is increased to 4. |
3rd | i = 4 northward = 5 | true | four is printed. i is increased to 5. |
4th | i = 5 n = v | truthful | half dozen is printed. i is increased to 6. |
5th | i = 6 n = v | false | The loop is terminated |
Example 4: Sum of Positive Numbers
// Java program to find the sum of positive numbers import java.util.Scanner; class Primary { 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 practise { // add only positive numbers sum += number; Organization.out.println("Enter a number"); number = input.nextInt(); } while(number >= 0); Organization.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
Hither, the user enters a positive number, that number is added to the sum variable. And this procedure 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 status volition be false
but the code inside of the loop executes once.
Infinite while loop
If the condition of a loop is e'er true
, the loop runs for infinite times (until the retentivity is total). For instance,
// infinite while loop while(true){ // torso of loop }
Here is an case of an infinite do...while
loop.
// space do...while loop int count = 1; exercise { // body of loop } while(count == 1)
In the above programs, the textExpression is e'er truthful
. Hence, the loop trunk will run for infinite times.
for and while loops
The for
loop is used when the number of iterations is known. For case,
for (permit i = ane; 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) { // body of loop }
Source: https://www.programiz.com/java-programming/do-while-loop
0 Response to "Would You Like to Run the Program Again Java Methods"
Post a Comment