Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax
3.
How does a While loop execute?
4.
Flow Diagram of while loop
5.
Examples
5.1.
Example 1: Printing numbers from 1 to 5
5.2.
C++
5.3.
Example 2: Sum of numbers until the user enters 0
5.4.
C++
6.
Frequently Asked Questions
6.1.
What happens if the condition in a while loop is always true?
6.2.
Can we have multiple statements inside the while loop?
6.3.
Is it necessary to have an increment or decrement statement inside the while loop?
7.
Conclusion
Last Updated: Jun 13, 2024
Easy

While Loop in C++

Author Ravi Khorwal
0 upvote

Introduction

In computer programming, loops are essential tools that allow you to repeat a block of code multiple times. One of the most commonly used loops in C++ is the while loop. It provides a simple & effective way to execute a set of instructions as long as a specific condition remains true. 

While Loop in C++

In this article, we will learn about the basics of the while loop, understand its syntax. We will also see its flowchart to understand how it works and end our article with few examples.

Syntax

The syntax of a while loop in C++ is straightforward. Here's how it looks:

while (condition) {
    // code block to be executed
}


The while keyword is followed by a condition enclosed in parentheses. The condition is an expression that evaluates to either true or false. If the condition is true, the code block inside the curly braces {} is executed. After the code block is executed, the condition is checked again. If it is still true, the code block is executed once more. This process continues until the condition becomes false.

How does a While loop execute?

  1. The condition is evaluated before entering the loop. If the condition is true, the code block is executed. If the condition is false from the beginning, the code block is skipped entirely, and the program continues with the next statement after the loop.
     
  2. Inside the loop, the code block is executed statement by statement. These statements can include variable assignments, function calls, or any other valid C++ code.
     
  3. After executing the last statement in the code block, the program goes back to the beginning of the loop and re-evaluates the condition.
     
  4. If the condition is still true, the code block is executed again. This process repeats until the condition becomes false.
     
  5. Once the condition becomes false, the loop is terminated, and the program continues with the next statement after the loop.
     

Note : It's crucial to ensure that the condition eventually becomes false; otherwise, the loop will continue indefinitely, resulting in an infinite loop.

Flow Diagram of while loop

Flow Diagram of while loop:

The flow diagram showing the following steps:

  1. The condition is evaluated. If it is true, the code block is executed. If it is false, the loop is terminated, and the program continues with the next statement after the loop.
     
  2. After executing the code block, the program goes back to the condition and re-evaluates it.
     
  3. If the condition is still true, the code block is executed again. This process repeats until the condition becomes false.
     
  4. Once the condition becomes false, the loop is terminated, and the program continues with the next statement after the loop.

Examples

Example 1: Printing numbers from 1 to 5

  • C++

C++

#include <iostream>

using namespace std;

int main() {

   int i = 1;

   while (i <= 5) {

       cout << i << " ";

       i++;

   }

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

1 2 3 4 5


In this example, the variable i is initialized to 1. The while loop continues to execute as long as i is less than or equal to 5. Inside the loop, the value of i is printed, and then i is incremented by 1. The loop terminates when i becomes 6.

Example 2: Sum of numbers until the user enters 0

  • C++

C++

#include <iostream>

using namespace std;

int main() {

   int number, sum = 0;

     cout << "Enter numbers (enter 0 to stop): ";

   while (true) {

       cin >> number;

       if (number == 0)

           break;

       sum += number;

   }

      cout << "Sum = " << sum << endl;

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


Output:

Enter numbers (enter 0 to stop): 5 8 3 2 0
Sum = 18


In this example, we use a while loop with a true condition to keep accepting numbers from the user until they enter 0. Inside the loop, we check if the entered number is 0. If it is, we use the break statement to exit the loop. Otherwise, we add the number to the sum variable. After the loop ends, we print the sum of all the entered numbers.

Frequently Asked Questions

What happens if the condition in a while loop is always true?

If the condition in a while loop is always true, it becomes an infinite loop, and the program will continue to execute indefinitely unless there is a break statement or some other way to exit the loop.

Can we have multiple statements inside the while loop?

Yes, you can have multiple statements inside the while loop. All the statements inside the loop will be executed repeatedly as long as the condition remains true.

Is it necessary to have an increment or decrement statement inside the while loop?

No, it's not necessary to have an increment or decrement statement inside the while loop. However, it's important to ensure that the condition eventually becomes false to avoid an infinite loop.

Conclusion

In this article, we learned about the while loop in C++. We discussed its syntax, understood how it executes, and looked at its flow diagram. We also saw practical examples of using the while loop to perform repetitive tasks. The while loop is a powerful tool that allows you to execute a block of code repeatedly based on a condition. 

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass