C++ While loopLast 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. SyntaxIt has the following syntax: Where,
Flow Diagram of While Loop![]()
C++ While Loop ExamplesPrint integer value from 1 to 10 using While LoopLet us take a simple example of a while loop to print number 1 to 10. ExampleCompile and RunOutput: 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:ExampleCompile and RunOutput: 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 ExampleLet us take a simple example of a nested while loop in C++. ExampleCompile and RunOutput: 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++ExampleCompile and RunOutput: 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 LoopWe 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 LoopLet us take a simple example to illustrate the C++ Infinitive While Loop: ExampleCompile and RunOutput: Infinitive While Loop Infinitive While Loop Infinitive While Loop Infinitive While Loop Infinitive While Loop Print pyramids using Infinite while loop in C++ExampleCompile and RunOutput: * * * * * * * * * * * * * * * Controlling While Loop Execution:
The break statement immediately exits the loop. ExampleCompile and RunOutput: 1 2 3 4
The continue statement skips the current iteration and moves to the next. ExampleCompile and RunOutput: 1 2 3 4 5 6 7 8 9 10 ConclusionIn 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. Next TopicC++ Do-While Loop |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
