Some Loop Programs In C++

C++ while Loop

The syntax of a while loop is:

while (testExpression) 
{
     // codes  
}

where, testExpression is checked on each entry of the while loop.


How while loop works?

  • The while loop evaluates the test expression.
  • If the test expression is true, codes inside the body of while loop is evaluated.
  • Then, the test expression is evaluated again. This process goes on until the test expression is false.
  • When the test expression is false, while loop is terminated.

Flowchart of while Loop

Flowchart of while loop in C++ Programming

Program 1: C++ while Loop

// C++ Program to compute factorial of a number
// Factorial of n = 1*2*3...*n

#include <iostream>
using namespace std;

int main() 
{
    int number, i = 1, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> number;
    
    while ( i <= number) {
        factorial *= i;      //factorial = factorial * i;
        ++i;
    }

    cout<<"Factorial of "<< number <<" = "<< factorial;
    return 0;
}

Output

Enter a positive integer: 4
Factorial of 4 = 24

In this program, user is asked to enter a positive integer which is stored in variable number. Let’s suppose, user entered 4.

Then, the while loop starts executing the code. Here’s how while loop works:

  1. Initially, i = 1, test expression i <= number is true and factorial becomes 1.
  2. Variable i is updated to 2, test expression is true, factorial becomes 2.
  3. Variable i is updated to 3, test expression is true, factorial becomes 6.
  4. Variable i is updated to 4, test expression is true, factorial becomes 24.
  5. Variable i is updated to 5, test expression is false and while loop is terminated.

C++ do…while Loop

The do…while loop is a variant of the while loop with one important difference. The body of do…while loop is executed once before the test expression is checked.

The syntax of do..while loop is:

do {
   // codes;
}
while (testExpression);

How do…while loop works?

  • The codes inside the body of loop is executed at least once. Then, only the test expression is checked.
  • If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false.
  • When the test expression is false, do…while loop is terminated.

Flowchart of do…while Loop

Flowchart of do while loop in C++ programming

Program 2: C++ do…while Loop

// C++ program to add numbers until user enters 0

#include <iostream>
using namespace std;

int main() 
{
    float number, sum = 0.0;
    
    do {
        cout<<"Enter a number: ";
        cin>>number;
        sum += number;
    }
    while(number != 0.0);

    cout<<"Total sum = "<<sum;
    
    return 0;
}

Output

Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: -4
Enter a number: 2
Enter a number: 4.4
Enter a number: 2
Enter a number: 0

C++ for Loop Syntax

for(initializationStatement; testExpression; updateStatement) {
    // codes 
}

where, only testExpression is mandatory.


How for loop works?

  1. The initialization statement is executed only once at the beginning.
  2. Then, the test expression is evaluated.
  3. If the test expression is false, for loop is terminated. But if the test expression is true, codes inside body of for loop is executed and update expression is updated.
  4. Again, the test expression is evaluated and this process repeats until the test expression is false.

Flowchart of for Loop in C++

Flowchart of for loop in C++ Programming

Program 3: C++ for Loop

// C++ Program to find factorial of a number
// Factorial on n = 1*2*3*...*n

#include <iostream>
using namespace std;

int main() 
{
    int i, n, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for (i = 1; i <= n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;
}

Output

Enter a positive integer: 5
Factorial of 5 = 120

In the program, user is asked to enter a positive integer which is stored in variable n(suppose user entered 5). Here is the working of for loop:

  1. Initially, i is equal to 1, test expression is true, factorial becomes 1.
  2. i is updated to 2, test expression is true, factorial becomes 2.
  3. i is updated to 3, test expression is true, factorial becomes 6.
  4. i is updated to 4, test expression is true, factorial becomes 24.
  5. i is updated to 5, test expression is true, factorial becomes 120.
  6. i is updated to 6, test expression is false, for loop is terminated.

In the above program, variable i is not used outside of the for loop. In such cases, it is better to declare the variable in for loop (at initialization statement).

#include <iostream>
using namespace std;

int main() 
{
    int n, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;
}

Leave a comment