In Java How to Start a Program Over Again
In computer programming, loops are used to repeat a cake of code. For example, if yous desire to show a bulletin 100 times, then you can use a loop. It's just a unproblematic example; yous can reach much more than with loops.
In the previous tutorial, you learned almost Coffee for loop. Here, y'all are going to learn about while
and do...while
loops.
Java while loop
Coffee while
loop is used to run a specific lawmaking until a certain condition is met. The syntax of the while
loop is:
while (testExpression) { // body of loop }
Hither,
- 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 about the conditions, visit Java relational and logical operators.
Flowchart of while loop
Instance 1: Brandish Numbers from ane to five
// Program to display numbers from ane to 5 class Main { public static void principal(Cord[] args) { // declare variables int i = ane, due north = 5; // while loop from 1 to v while(i <= n) { System.out.println(i); i++; } } }
Output
ane 2 3 4 v
Here is how this program works.
Iteration | Variable | Condition: i <= n | Action |
---|---|---|---|
1st | i = i n = 5 | true | ane is printed. i is increased to 2. |
2d | i = 2 n = 5 | true | 2 is printed. i is increased to 3. |
tertiary | i = 3 n = 5 | true | iii is printed. i is increased to 4. |
fourth | i = 4 n = 5 | truthful | 4 is printed. i is increased to 5. |
5th | i = 5 n = five | true | 5 is printed. i is increased to 6. |
6th | i = half dozen n = 5 | fake | The loop is terminated |
Instance 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 course Scanner input = new Scanner(Organisation.in); // accept integer input from the user Arrangement.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 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. 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 exercise...while loop
The do...while
loop is similar to while loop. However, the body of do...while
loop is executed once before the test expression is checked. For example,
do { // trunk of loop } while(textExpression);
Here,
- The body of the loop is executed at first. And then the textExpression is evaluated.
- If the textExpression evaluates to
true
, the body of the loop inside thedo
statement is executed again. - The textExpression is evaluated one time once again.
- If the textExpression evaluates to
true
, the body of the loop within thedo
statement is executed again. - This process continues until the textExpression evaluates to
simulated
. Then the loop stops.
Flowchart of exercise...while loop
Let's run into the working of do...while
loop.
Case 3: Display Numbers from 1 to 5
// Java Program to display numbers from 1 to v import coffee.util.Scanner; // Program to find the sum of natural numbers from 1 to 100. class Master { public static void principal(String[] args) { int i = 1, n = 5; // do...while loop from 1 to 5 do { System.out.println(i); i++; } while(i <= n); } }
Output
1 2 3 4 5
Here is how this program works.
Iteration | Variable | Condition: i <= northward | Action |
---|---|---|---|
i = 1 n = 5 | not checked | 1 is printed. i is increased to 2. | |
1st | i = 2 n = five | true | 2 is printed. i is increased to 3. |
2nd | i = 3 n = v | true | three is printed. i is increased to 4. |
3rd | i = 4 due north = v | true | 4 is printed. i is increased to 5. |
quaternary | i = v n = v | truthful | 6 is printed. i is increased to 6. |
5th | i = half dozen n = five | false | The loop is terminated |
Example 4: Sum of Positive Numbers
// Java plan to detect the sum of positive numbers import coffee.util.Scanner; class Principal { public static void primary(String[] args) { int sum = 0; int number = 0; // create an object of Scanner class Scanner input = new Scanner(Organisation.in); // practise...while loop continues // until entered number is positive exercise { // add only positive numbers sum += number; System.out.println("Enter a number"); number = input.nextInt(); } while(number >= 0); System.out.println("Sum = " + sum); input.close(); } }
Output i
Enter a number 25 Enter a number nine 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 calculation the negative number.
Output 2
Enter a number -8 Sum is 0
Here, the user enters a negative number. The examination condition will be simulated
merely the code within of the loop executes once.
Space while loop
If the condition of a loop is always true
, the loop runs for infinite times (until the memory is total). For example,
// space while loop while(true){ // body of loop }
Here is an case of an space practice...while
loop.
// infinite do...while loop int count = 1; practice { // trunk of loop } while(count == 1)
In the above programs, the textExpression is e'er true
. 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 instance,
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) { // torso of loop }
Source: https://www.programiz.com/java-programming/do-while-loop
0 Response to "In Java How to Start a Program Over Again"
Mag-post ng isang Komento