WikiGalaxy

Personalize

Introduction to C++ While Loop

Understanding the While Loop:

The while loop in C++ is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The code inside the loop is executed as long as the condition is true.

Basic Syntax of While Loop

Syntax:

The basic syntax of a while loop in C++ is as follows:


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

Example 1: Simple Counter

Counting from 1 to 5:

This example demonstrates a simple counter using a while loop that prints numbers from 1 to 5.


#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << endl;
        i++;
    }
    return 0;
}
            

Console Output:

1 2 3 4 5

Example 2: User Input Validation

Prompting Until Correct Input:

This example shows how a while loop can be used to repeatedly prompt the user for input until a valid number is entered.


#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a positive number: ";
    cin >> number;
    
    while (number <= 0) {
        cout << "Invalid! Enter a positive number: ";
        cin >> number;
    }
    cout << "You entered: " << number << endl;
    return 0;
}
            

Console Output:

Enter a positive number: -3 Invalid! Enter a positive number: -1 Invalid! Enter a positive number: 5 You entered: 5

Example 3: Summing Numbers

Calculating the Sum of Numbers:

This example calculates the sum of numbers from 1 to 10 using a while loop.


#include <iostream>
using namespace std;

int main() {
    int sum = 0;
    int i = 1;
    
    while (i <= 10) {
        sum += i;
        i++;
    }
    cout << "Sum from 1 to 10 is: " << sum << endl;
    return 0;
}
            

Console Output:

Sum from 1 to 10 is: 55

Example 4: Factorial Calculation

Computing Factorial of a Number:

This example demonstrates using a while loop to calculate the factorial of a number.


#include <iostream>
using namespace std;

int main() {
    int n = 5;
    int factorial = 1;
    
    while (n > 0) {
        factorial *= n;
        n--;
    }
    cout << "Factorial is: " << factorial << endl;
    return 0;
}
            

Console Output:

Factorial is: 120

Example 5: Infinite Loop

Creating an Infinite Loop:

An infinite loop occurs when the loop's termination condition never becomes false, causing the loop to run indefinitely. This can be useful in certain cases like waiting for user input or server requests.


#include <iostream>
using namespace std;

int main() {
    while (true) {
        cout << "This will run forever." << endl;
    }
    return 0;
}
            

Note:

Use Ctrl+C to stop the execution in a terminal.

Example 6: Nested While Loops

Using Nested Loops:

Nested loops are loops within loops. Each time the outer loop runs, the inner loop is re-executed from the beginning.


#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 3) {
        int j = 1;
        while (j <= 2) {
            cout << "i = " << i << ", j = " << j << endl;
            j++;
        }
        i++;
    }
    return 0;
}
            

Console Output:

i = 1, j = 1 i = 1, j = 2 i = 2, j = 1 i = 2, j = 2 i = 3, j = 1 i = 3, j = 2

Example 7: Break Statement

Exiting a Loop Prematurely:

The break statement allows you to exit a loop when a certain condition is met, even if the loop's condition is still true.


#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 10) {
        if (i == 6) {
            break;
        }
        cout << i << endl;
        i++;
    }
    return 0;
}
            

Console Output:

1 2 3 4 5

Example 8: Continue Statement

Skipping an Iteration:

The continue statement skips the current iteration of the loop and proceeds to the next iteration.


#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while (i < 10) {
        i++;
        if (i % 2 == 0) {
            continue;
        }
        cout << i << endl;
    }
    return 0;
}
            

Console Output:

1 3 5 7 9

Example 9: Do-While Loop

Difference with Do-While Loop:

The do-while loop is similar to the while loop, but it checks the condition after executing the loop's body, ensuring that the loop is executed at least once.


#include <iostream>
using namespace std;

int main() {
    int i = 1;
    do {
        cout << i << endl;
        i++;
    } while (i <= 5);
    return 0;
}
            

Console Output:

1 2 3 4 5

Example 10: While Loop with Arrays

Iterating Over an Array:

This example demonstrates using a while loop to iterate over an array and print its elements.


#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int i = 0;
    
    while (i < 5) {
        cout << arr[i] << endl;
        i++;
    }
    return 0;
}
            

Console Output:

10 20 30 40 50

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025