Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Understanding While Loop in C++

Understanding While Loop in C++

21 May 2024
Beginner
3.34K Views
9 min read
Learn with an interactive course and practical hands-on labs

Free C++ Course: Learn C++ In 21 Days

While Loop in C++: An Overview

We have already become familiar with the concept of loop and for loopin C++ programming in the previous tutorial, Loops in C++. In this C++ tutorial, we are going to look at another important loop, while loop in C++. To strengthen your C++ programming skills, just consider our C++ Certification program and become a certified C++ programmer.

While Loop in C++

It repeatedly carries out a series of instructions till a condition is true. It is an entry-controlled loop. The while loop in C++ is used when we don’t know the number of iterations.

While Loop in C++

Syntax

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

If the testCondition inside the () becomes true, the body of the loop executes else loop terminates without execution. The process repeats until the testCondition becomes false.

Example to demonstrate while loop in C++

 #include <iostream>
using namespace std;
int main() {
 int i = 1;
 while (i <= 10) {
 cout << i << endl;
 i++;
 }
 return 0;
}
  • In the above code in C++ Compiler, i is initialized to 1 before the start of the loop.
  • The testCondition, i<=10 is evaluated. If true, the body of the loop executes.
  • If the condition becomes false in the beginning itself, the program control does not even enter the loop once.
  • The loop executes until i becomes 10.

Output

1
2
3
4
5
6
7
8
9
10

Read More - C++ Interview Interview Questions for Experienced

Nested While Loop in C++

It is any type of loop inside a whileloop.

Nested While Loop in C++

Example to demonstrate Nested do...while loop in C++

 #include <iostream>
using namespace std;
int main() {
 int i = 1, j = 1;
 while (i <= 3) {
 cout << "Outer loop iteration " << i << endl;
 while (j <= 3) {
 cout << " Inner loop iteration " << j << endl;
 j++;
 }
 j = 1; // reset j to 1 for the next iteration of the outer loop
 i++;
 }
 return 0;
}
  • The inner loop j iterates within each iteration of the outer loop i, printing "Inner loop iteration" messages
  • The outer loop i iterates three times, printing "Outer loop iteration" messages.
  • After each inner loop is finished, j is reset to 1, resulting in nested iteration.

Output

Outer loop iteration 1
 Inner loop iteration 1
 Inner loop iteration 2
 Inner loop iteration 3
Outer loop iteration 2
 Inner loop iteration 1
 Inner loop iteration 2
 Inner loop iteration 3
Outer loop iteration 3
 Inner loop iteration 1
 Inner loop iteration 2
 Inner loop iteration 3

Infinite while loop in C++

If the condition of a loop is always true, the loop becomes an infinite one.

Syntax

while(true) 
{ 
 // body of the loop.. 
} 

Example to demonstrate infinite do...while loop in C++

#include <iostream>
using namespace std;
int main() {
 char str[] = "Infinite while loop";
 int i = 0;

 while (true) {
 i++;
 cout << "i is: " << i << endl;
 }

 // The return statement may never be reached in an infinite loop.
 return 0;
}

Here, the value of i will be printed n number of times due to the absence of a terminating condition.

Output

i is: 1
i is: 2
i is: 3
i is: 4
i is: 5
... 
Summary
So, in this article, we have analyzed the while loop from all angles and have understood its applications in various scenarios. If you want to further understand the loop and its application in various C++ programs, you can consider our C++ Certification program.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this