while loop

While loop in C++

The while loop is one of the fundamental repetition tools in C++, and this guide covers its syntax, the do-while variant, and how to avoid common mistakes.

When you look for the successor of the C language, then you will find C++ and if you are familiar with the C language and want to upgrade yourself in the world of programming, then C++ programming is right for you. Now, this C++ tutorial gives you a detailed overview of the basic and advanced concepts of C plus plus So, if you are a college student or a working professional, bookmark this C ++ programming tutorial to upscale your CPP programming skills. But before going in-depth with the C Plus Plus tutorial, let’s have a quick intro to C++ language.

What Is a While Loop?

A while loop is a control structure that repeats a block of code for as long as a given condition remains true. It is the most direct looping construct in C++, useful whenever the exact number of repetitions is not known in advance and instead depends on some condition being met while the program runs.

Basic Syntax

while (condition) {
    // executed repeatedly as long as condition is true
}

Before every single pass through the loop body, C++ checks the condition. If it evaluates to true, the body runs once and the condition is checked again; if it evaluates to false, the loop stops and execution moves to whatever comes after it. Because the check happens before the body ever runs, a while loop can execute zero times if the condition is already false on the very first check.

A Working Example

#include <iostream>
using namespace std;

int main() {
    int count = 1;

    while (count <= 5) {
        cout << "Count is: " << count << endl;
        count++;
    }

    return 0;
}

This prints the numbers 1 through 5, one per line. The variable count is initialized before the loop, checked against the condition count <= 5 on every pass, and incremented inside the loop body with count++. That increment step is essential – without it, count would never reach 6, the condition would never become false, and the loop would run forever.

The Do-While Variant

C++ also provides a do-while loop, a close relative of the while loop with one key difference: the condition is checked after the body runs, not before. This guarantees the loop body executes at least once, even if the condition is false from the start.

int number;

do {
    cout << "Enter a positive number: ";
    cin >> number;
} while (number <= 0);

Here, the prompt is shown at least once no matter what, and only repeats if the user enters a number that fails the condition, since the check happens only after the input has already been read.

Infinite Loops and How to Avoid Them

A while loop whose condition never becomes false will run forever, a bug commonly called an infinite loop. This typically happens when the variable controlling the condition is never updated inside the loop body, or is updated in a way that never actually satisfies the exit condition. Every while loop should be checked to confirm that something inside its body genuinely moves the condition toward becoming false.

Break and Continue Inside a While Loop

Two keywords give extra control over a running loop. break exits the loop immediately, regardless of whether the condition is still true, and is often used alongside a separate check inside the loop body:

int num = 1;
while (num <= 100) {
    if (num == 5) {
        break;
    }
    cout << num << " ";
    num++;
}

continue, by contrast, skips only the rest of the current pass through the loop body and jumps straight back to re-checking the condition, without exiting the loop entirely – useful for skipping specific values while still continuing the overall loop.

The natural next topic to learn is covered in our article on User Input in C++. For the full, authoritative language reference, cppreference.com documents this in complete technical detail.

No Comments

Leave a Reply

Recent Comments

No comments to show.