Cout << Do You Want to Go Back to Menu (Y/n)? ; Cin >> Again; Cout << Endl << Endl << Endl;

In figurer programming, loops are used to repeat a block of lawmaking.

For instance, allow'due south say we want to show a message 100 times. So instead of writing the print statement 100 times, we can use a loop.

That was just a simple example; nosotros tin reach much more efficiency and sophistication in our programs by making effective apply of loops.

There are 3 types of loops in C++.

  1. for loop
  2. while loop
  3. practice...while loop

In the previous tutorial, we learned near the C++ for loop. Here, nosotros are going to larn most while and practise...while loops.


C++ while Loop

The syntax of the while loop is:

          while (condition) {     // trunk of the loop }        

Here,

  • A while loop evaluates the condition
  • If the condition evaluates to true, the lawmaking inside the while loop is executed.
  • The condition is evaluated again.
  • This process continues until the status is faux.
  • When the status evaluates to faux, the loop terminates.

To learn more about the conditions, visit C++ Relational and Logical Operators.


Flowchart of while Loop

C++ while loop flowchart
Flowchart of C++ while loop

Instance 1: Display Numbers from ane to 5

          // C++ Program to print numbers from 1 to five  #include <iostream>  using namespace std;  int primary() {     int i = 1;       // while loop from i to five     while (i <= 5) {         cout << i << " ";         ++i;     }          return 0; }        

Output

          1 ii 3 four 5        

Hither is how the plan works.

Iteration Variable i <= 5 Activity
1st i = ane true 1 is printed and i is increased to two.
2nd i = 2 true two is printed and i is increased to 3.
3rd i = 3 true 3 is printed and i is increased to 4
4th i = four true iv is printed and i is increased to 5.
5th i = 5 true five is printed and i is increased to half dozen.
sixth i = 6 false The loop is terminated

Case 2: Sum of Positive Numbers Only

          // program 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;     int sum = 0;      // have input from the user     cout << "Enter a number: ";     cin >> number;      while (number >= 0) {         // add all positive numbers         sum += number;          // take input once 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: six Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -2  The sum is 25        

In this programme, the user is prompted to enter a number, which is stored in the variable number.

In club to store the sum of the numbers, nosotros 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 full sum is displayed.


C++ do...while Loop

The exercise...while loop is a variant of the while loop with one important difference: the torso of exercise...while loop is executed once before the condition is checked.

Its syntax is:

          do {    // body of loop; } while (status);        

Here,

  • The body of the loop is executed at get-go. Then the condition is evaluated.
  • If the status evaluates to truthful, the body of the loop inside the practice argument is executed again.
  • The condition is evaluated once once again.
  • If the condition evaluates to true, the body of the loop inside the do statement is executed once more.
  • This procedure continues until the condition evaluates to false. Then the loop stops.

Flowchart of exercise...while Loop

C++ do...while loop flowchart
Flowchart of C++ do...while loop

Example 3: Display Numbers from 1 to 5

          // C++ Programme to print numbers from 1 to 5  #include <iostream>  using namespace std;  int main() {     int i = 1;       // do...while loop from 1 to 5     do {         cout << i << " ";         ++i;     }     while (i <= 5);          render 0; }        

Output

          i 2 iii 4 5        

Here is how the program works.

Iteration Variable i <= 5 Action
i = ane not checked ane is printed and i is increased to 2
1st i = 2 true 2 is printed and i is increased to iii
2nd i = 3 true 3 is printed and i is increased to iv
3rd i = 4 true 4 is printed and i is increased to 5
4th i = five true v is printed and i is increased to six
5th i = 6 faux The loop is terminated

Example 4: Sum of Positive Numbers Only

          // program 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 master() {     int number = 0;     int sum = 0;      do {         sum += number;          // accept input from the user         cout << "Enter a number: ";         cin >> number;     }     while (number >= 0);          // display the sum     cout << "\nThe sum is " << sum << endl;          return 0; }        

Output 1

          Enter a number: 6 Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -two  The sum is 25        

Here, the exercise...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 2

          Enter a number: -6 The sum is 0.        

The body 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 retentivity is full). For example,

          // infinite while loop while(truthful) {     // body of the loop }        

Here is an instance of an infinite do...while loop.

          // space do...while loop  int count = 1;  practice {    // body of loop }  while(count == 1);        

In the higher up programs, the status is always true. Hence, the loop body volition run for space times.


for vs while loops

A for loop is commonly used when the number of iterations is known. For example,

          // This loop is iterated 5 times for (int i = 1; i <=5; ++i) {    // trunk of the loop }        

Hither, nosotros know that the for-loop volition be executed 5 times.

Even so, while and do...while loops are usually used when the number of iterations is unknown. For example,

          while (condition) {     // body of the loop }        

Check out these examples to learn more:

  • C++ Program to Display Fibonacci Series
  • C++ Program to Notice GCD
  • C++ Plan to Find LCM

lanesual1938.blogspot.com

Source: https://www.programiz.com/cpp-programming/do-while-loop

0 Response to "Cout << Do You Want to Go Back to Menu (Y/n)? ; Cin >> Again; Cout << Endl << Endl << Endl;"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel