While Loop to Run Program Again Java
In figurer programming, loops are used to repeat a cake of code.
For example, let'southward say we want to show a message 100 times. And so instead of writing the print argument 100 times, we tin can use a loop.
That was just a simple case; nosotros can achieve much more efficiency and sophistication in our programs by making constructive use of loops.
There are 3 types of loops in C++.
-
for
loop -
while
loop -
do...while
loop
In the previous tutorial, nosotros learned nigh the C++ for loop. Here, we are going to learn virtually while
and do...while
loops.
C++ while Loop
The syntax of the while
loop is:
while (condition) { // body of the loop }
Here,
- A
while
loop evaluates thestatus
- If the
condition
evaluates totrue
, the code inside thewhile
loop is executed. - The
condition
is evaluated again. - This process continues until the
condition
issimulated
. - When the
condition
evaluates tofalse
, the loop terminates.
To learn more near the conditions
, visit C++ Relational and Logical Operators.
Flowchart of while Loop

Example 1: Display Numbers from 1 to v
// C++ Program to print numbers from i to five #include <iostream> using namespace std; int main() { int i = one; // while loop from 1 to 5 while (i <= 5) { cout << i << " "; ++i; } return 0; }
Output
1 ii three 4 5
Hither is how the program works.
Iteration | Variable | i <= v | Activity |
---|---|---|---|
1st | i = one | true | 1 is printed and i is increased to 2 . |
second | i = 2 | truthful | 2 is printed and i is increased to 3 . |
3rd | i = 3 | true | 3 is printed and i is increased to 4 |
quaternary | i = iv | truthful | four is printed and i is increased to 5 . |
5th | i = 5 | truthful | 5 is printed and i is increased to six . |
6th | i = 6 | imitation | The loop is terminated |
Example two: Sum of Positive Numbers Merely
// programme to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int principal() { int number; int sum = 0; // take input from the user cout << "Enter a number: "; cin >> number; while (number >= 0) { // add all positive numbers sum += number; // take input again if the number is positive cout << "Enter a number: "; cin >> number; } // display the sum cout << "\nThe sum is " << sum << endl; return 0; }
Output
Enter a number: vi Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -2 The sum is 25
In this program, the user is prompted to enter a number, which is stored in the variable number.
In social club to store the sum of the numbers, we declare a variable sum and initialize it to the value of 0
.
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.
C++ do...while Loop
The do...while
loop is a variant of the while
loop with i important difference: the body of do...while
loop is executed once earlier the status
is checked.
Its syntax is:
do { // body of loop; } while (status);
Here,
- The trunk of the loop is executed at offset. Then the
condition
is evaluated. - If the
condition
evaluates totruthful
, the body of the loop inside thepractise
argument is executed again. - The
status
is evaluated once once more. - If the
condition
evaluates totrue
, the body of the loop inside thepractise
statement is executed again. - This process continues until the
condition
evaluates tosimulated
. Then the loop stops.
Flowchart of exercise...while Loop

Example 3: Display Numbers from 1 to five
// C++ Program to print numbers from one to 5 #include <iostream> using namespace std; int master() { int i = 1; // do...while loop from ane to 5 exercise { cout << i << " "; ++i; } while (i <= v); return 0; }
Output
1 2 3 4 5
Here is how the program works.
Iteration | Variable | i <= 5 | Action |
---|---|---|---|
i = 1 | not checked | one is printed and i is increased to 2 | |
1st | i = 2 | truthful | 2 is printed and i is increased to 3 |
second | i = 3 | true | 3 is printed and i is increased to 4 |
3rd | i = iv | true | 4 is printed and i is increased to v |
4th | i = 5 | truthful | 5 is printed and i is increased to 6 |
5th | i = 6 | false | The loop is terminated |
Case four: Sum of Positive Numbers Merely
// plan to find the sum of positive numbers // If the user enters a negative number, the loop ends // the negative number entered is non added to the sum #include <iostream> using namespace std; int main() { int number = 0; int sum = 0; practise { sum += number; // take input from the user cout << "Enter a number: "; cin >> number; } while (number >= 0); // display the sum cout << "\nThe sum is " << sum << endl; render 0; }
Output one
Enter a number: vi Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -2 The sum is 25
Hither, the practise...while
loop continues until the user enters a negative number. When the number is negative, the loop terminates; the negative number is not added to the sum
variable.
Output ii
Enter a number: -6 The sum is 0.
The trunk of the do...while
loop runs only once if the user enters a negative number.
Infinite while loop
If the condition
of a loop is always true
, the loop runs for infinite times (until the memory is full). For example,
// infinite while loop while(true) { // torso of the loop }
Here is an instance of an space do...while
loop.
// infinite do...while loop int count = one; do { // trunk of loop } while(count == one);
In the to a higher place programs, the condition
is ever truthful
. Hence, the loop body will run for infinite times.
for vs while loops
A for
loop is ordinarily used when the number of iterations is known. For example,
// This loop is iterated 5 times for (int i = 1; i <=five; ++i) { // trunk of the loop }
Here, we know that the for-loop will be executed 5 times.
Even so, while
and do...while
loops are ordinarily used when the number of iterations is unknown. For example,
while (condition) { // body of the loop }
Check out these examples to learn more than:
- C++ Program to Display Fibonacci Series
- C++ Program to Find GCD
- C++ Plan to Find LCM
Source: https://www.programiz.com/cpp-programming/do-while-loop
0 Response to "While Loop to Run Program Again Java"
Post a Comment