C++ while Loop

Hi everyone, inside this article we will see about C++ while loop Statement.

The while loop is a control structure in C++ which is used to execute a block of code repeatedly as long as a specified condition is true.

Syntax & Explanation

It is defined using the following syntax:

while (condition) {
   statement(s);
}

condition: This is evaluated before each iteration of the loop. If the condition is true, the loop continues to execute. If it’s false, the loop stops executing.

statement(s): This is the body of the loop, executed repeatedly as long as the condition is true.

Here, a pictorial representation of while loop statement:

A Basic Program

Here’s a simple program in C++ that demonstrates the use of a while loop:

#include <iostream>
using namespace std;

int main() {
   int i = 1;
   while (i <= 10) {
      cout << i << " ";
      i++;
   }
   cout << endl;

   return 0;
}

The output of this program will be:

1 2 3 4 5 6 7 8 9 10

In this program, the while loop iterates 10 times (from 1 to 10), printing each number to the screen. The condition i <= 10 is evaluated before each iteration, and the loop continues as long as i is less than or equal to 10. The value of i is incremented by 1 in each iteration using the i++ statement.

C++ Nested while Loop

A nested while loop in C++ is a while loop inside another while loop. The inner while loop is executed completely for each iteration of the outer while loop. This allows you to perform a task multiple times, nested inside another task that is also being repeated.

Here’s an example of a nested while loop in C++:

#include <iostream>
using namespace std;

int main() {
   int i = 1;
   while (i <= 3) {
      int j = 1;
      while (j <= 3) {
         cout << i * j << " ";
         j++;
      }
      cout << endl;
      i++;
   }

   return 0;
}

The output of this program will be:

1 2 3
2 4 6
3 6 9

In this program, the outer while loop iterates three times (from 1 to 3) and the inner while loop iterates three times for each iteration of the outer loop. The product of i and j is printed to the screen, resulting in a 3×3 multiplication table. The inner loop continues as long as j is less than or equal to 3 and the outer loop continues as long as i is less than or equal to 3.

C++ Infinitive while Loop

An infinite while loop in C++ is a while loop that never terminates, it runs forever. It is created by having a condition in the while loop that is always true.

Syntax

while (true) {
   statement(s);
}

Here is an example of an infinite while loop in C++:

#include <iostream>  
using namespace std;  

int main () {  
    while(true) {    
       cout << "Infinitive While Loop";    
    }
  
   return 0;
}

The output of this program will be:

Infinitive While Loop 
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
ctrl+c

We hope this article helped you to understand about C++ while loop in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.