C++ while Loop

Introduction

The while loop in C++ is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop continues to execute as long as the specified condition is true. The while loop is useful when the number of iterations is not known in advance and depends on a condition.

Syntax

The basic syntax of a while loop is as follows:

while (condition) {
    // code to be executed
}

Key Points

  • Condition: Evaluated before each iteration. If true, the loop body is executed. If false, the loop terminates.
  • Loop Body: The block of code that is executed repeatedly as long as the condition is true.

Example: Basic while Loop

#include <iostream>
using namespace std;

int main() {
    int i = 0; // Initialization of loop control variable

    while (i < 5) { // Condition
        cout << "i = " << i << endl; // Print the value of i
        i++; // Increment the loop control variable
    }

    return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4

Explanation

  • Initialization: int i = 0 initializes the loop control variable i to 0.
  • Condition: i < 5 checks if i is less than 5. If true, the loop body executes.
  • Loop Body: cout << "i = " << i << endl; prints the value of i.
  • Increment: i++ increments i by 1 after each iteration.

Example: Sum of First N Natural Numbers

A while loop can be used to calculate the sum of the first N natural numbers.

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0, i = 1; // Initialization of variables

    cout << "Enter a positive integer: ";
    cin >> n; // Input the value of n

    while (i <= n) { // Condition
        sum += i; // Add i to sum
        i++; // Increment i
    }

    cout << "Sum of the first " << n << " natural numbers is: " << sum << endl;

    return 0;
}

Output

Enter a positive integer: 10
Sum of the first 10 natural numbers is: 55

Explanation

  • Initialization: int n, sum = 0, i = 1 initializes the variables.
  • Input: cin >> n takes the input value of n.
  • Condition: i <= n checks if i is less than or equal to n. If true, the loop body executes.
  • Loop Body: sum += i adds i to sum. i++ increments i by 1 after each iteration.

Example: Find Factorial of a Number

A while loop can be used to calculate the factorial of a given number.

#include <iostream>
using namespace std;

int main() {
    int n, factorial = 1, i = 1; // Initialization of variables

    cout << "Enter a positive integer: ";
    cin >> n; // Input the value of n

    while (i <= n) { // Condition
        factorial *= i; // Multiply i to factorial
        i++; // Increment i
    }

    cout << "Factorial of " << n << " is: " << factorial << endl;

    return 0;
}

Output

Enter a positive integer: 5
Factorial of 5 is: 120

Explanation

  • Initialization: int n, factorial = 1, i = 1 initializes the variables.
  • Input: cin >> n takes the input value of n.
  • Condition: i <= n checks if i is less than or equal to n. If true, the loop body executes.
  • Loop Body: factorial *= i multiplies i to factorial. i++ increments i by 1 after each iteration.

Example: Reverse Digits of a Number

A while loop can be used to reverse the digits of a given number.

#include <iostream>
using namespace std;

int main() {
    int number, reversedNumber = 0; // Initialization of variables

    cout << "Enter an integer: ";
    cin >> number; // Input the value of number

    while (number != 0) { // Condition
        int remainder = number % 10; // Get the last digit
        reversedNumber = reversedNumber * 10 + remainder; // Build the reversed number
        number /= 10; // Remove the last digit
    }

    cout << "Reversed Number: " << reversedNumber << endl;

    return 0;
}

Output

Enter an integer: 1234
Reversed Number: 4321

Explanation

  • Initialization: int number, reversedNumber = 0 initializes the variables.
  • Input: cin >> number takes the input value of number.
  • Condition: number != 0 checks if number is not equal to 0. If true, the loop body executes.
  • Loop Body:
    • int remainder = number % 10 gets the last digit of number.
    • reversedNumber = reversedNumber * 10 + remainder builds the reversed number.
    • number /= 10 removes the last digit from number.

Conclusion

The while loop is a powerful and flexible control flow statement in C++ that allows you to execute a block of code repeatedly based on a given condition. This chapter covered the basic syntax of the while loop, examples of calculating the sum of the first N natural numbers, finding the factorial of a number, and reversing the digits of a number. Understanding how to use the while loop effectively will help you write more efficient and readable code. In the next chapter, we will explore the do-while loop in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top