C++ While loop

Last Updated : 10 Jan 2026

In C++ programming language, loops are an important concept to iterate over a group of statements until a specific condition is met. While loop is one of the loops that is known for its simplicity and versatility.

In C++, a while loop is used to iterate a part of the program several times. If the number of iterations is not fixed, it is recommended to use a while loop than for loop.

Syntax

It has the following syntax:

Where,

  1. Condition: It is an expression that checks the given condition. If the condition is true, the loop executes its body. If the condition is false, it exits the loop.
  2. Update Expression: It updates the loop variable value, whether it increments or decrements.
  3. Body: It is the main body of the program that contains a set of statements that would be executed until the condition remains true.

Flow Diagram of While Loop

Cpp While loop 1
  • Start
  • Initialize a variable
  • Check the loop condition
  • If the condition is true:
    • Execute the loop body
    • Modify the variable
    • Repeat condition check
  • If the condition is false, exit the loop
  • End

C++ While Loop Examples

Print integer value from 1 to 10 using While Loop

Let us take a simple example of a while loop to print number 1 to 10.

Example

Compile and Run

Output:

1
2
3
4
5
6
7
8
9
10

Explanation:

In this example, we print the integer value from 1 to 10 using a while loop. Here, we initialize an integer value (i=1). After that, it checks the condition (1<=10) in the loop. If the condition is true, it prints the value of i. If the condition is false, it exits the loop.

Factorial Number Program using While Loop:

Example

Compile and Run

Output:

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

C++ Nested While Loop:

In C++, we can use a while loop inside another while loop, which is known as a nested while loop. The nested while loop is executed fully when the outer loop is executed once.

Simple Nested while Loop Example

Let us take a simple example of a nested while loop in C++.

Example

Compile and Run

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Print Multiplication Table using While Loop in C++

Example

Compile and Run

Output:

Enter the starting number: 2
Enter the ending number: 3
Multiplication Table of 2: 
2 x 1 = 2 
2 x 2 = 4 
2 x 3 = 6 
2 x 4 = 8 
2 x 5 = 10 
2 x 6 = 12 
2 x 7 = 14 
2 x 8 = 16 
2 x 9 = 18 
2 x 10 = 20 
Multiplication Table of 3: 
3 x 1 = 3 
3 x 2 = 6 
3 x 3 = 9 
3 x 4 = 12 
3 x 5 = 15 
3 x 6 = 18 
3 x 7 = 21 
3 x 8 = 24 
3 x 9 = 27 
3 x 10 = 30

C++ Infinitive While Loop

We can also create an infinite while loop by passing true as the test condition. It means that if we pass the condition to true (while(true)) in the while loop, it would be an infinitive while loop.

Syntax:

It has the following syntax:

In this syntax, this loop will continue because the condition is always true.

Simple Example for Infinite While Loop

Let us take a simple example to illustrate the C++ Infinitive While Loop:

Example

Compile and Run

Output:

Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop

Print pyramids using Infinite while loop in C++

Example

Compile and Run

Output:

* 
* *
* * * 
* * * * 
* * * * *

Controlling While Loop Execution:

  • Using break Statement:

The break statement immediately exits the loop.

Example

Compile and Run

Output:

1
2
3
4
  • Using continue Statement:

The continue statement skips the current iteration and moves to the next.

Example

Compile and Run

Output:

1
2
3
4
5
6
7
8
9
10

Conclusion

In C++, the while loop is an essential tool for repeating actions in C++ when the number of iterations is unknown. Mastering while do-while and nested loops enables developers to write efficient and flexible programs. Proper understanding of loop termination, optimization techniques, and best practices can lead to better performance and maintainability in programming.