Introduction

A loop is a programming statement that tells the computer to do something over and over again until certain conditions are met.

In this article, we will study the C++ while loop: What is its definition? What is the syntax related to C++ while loop? And we will see some examples.

Looping in programming

The loop is one of the essential components of any programming language. It allows you to execute a set of instructions repeatedly, usually with some changes in the value of a variable or two.A loop is used when you want to do something more than once.

For example, if you print out all the numbers from 1-10 on your screen, it’s easier and faster to use a loop than to type out each number individually.

What is the C++ while loop?

What is the C++ while loop

A while loop is a control flow statement that allows you to execute a block of code repeatedly as long as the tested condition evaluates to true.

The while loop does not have any form of initialization or termination conditions. It starts with an expression and will continue executing the code block in the loop as long as that expression evaluates to true.

The syntax of C++ while loop

The while loop is a programming language control flow statement that allows code to be executed repeatedly until a given condition becomes false. C++ while loop is a pre-test loop since the test in the loop condition is accomplished first.

C++ while loop syntax is the following expression.A while loop starts with the keyword while followed by a condition and a block of code. The code inside the block is executed as long as the condition evaluates to true.

When the condition becomes false, the control flow jumps out of the loop and continues with whatever follows it in the program.

1

Syntax of C++ while loop

while (condition)statements;

2

Syntax of C++ while loop

while (condition) {statements;}

C++language is the largest in the world

C++ while loop

Syntax of C++ while loop

” We can consider this expression to be a logical expression. If the condition is true, it returns true. If the condition is not met, it returns a false.”

We can consider this expression to be a logical expression. If the condition is true, it returns true. If the condition is not met, it returns a false.

Benefits while loop

There are many benefits of using the while loop in programming. It’s an easy way to repeat a code block.

The while loop is a control structure that is used to execute a block of code repeatedly until a given condition becomes false. The while loop evaluates the condition before running the code and repeats it until it becomes false.The benefits of using the while loop are:

  • It’s an easy way to repeat a code block.
  • It’s flexible because you can change the condition at any time.
  • It doesn’t need to be initialized.

Example 1: simple C++ while loop

Introduce the program

We will build a C++ program that takes care of numbers that are divisible by 5We declare an integer called counter. The condition is true when the counter is less or equal to 100. Inside the while loop, we have an if statement.

The if statement condition is true when The remainder of dividing the counter by 5 equals 0. The C++ while loop checks and increments by one every time until the while loop becomes false. At this time, the program stops looping.

The code of the program

#include <iostream>using namespace std;int main() {int counter = {};while (counter <= 100) {if(counter % 5==0)cout << counter <<endl;counter++;}return 0;}

The output of the program

simple C++ while loop

Example2: array C++ while loop

Introduce the program

Remember that an array is a data structure that stores a collection of values. It can be stored in either the RAM or on the hard drive. The size of an array is determined by the number of elements it will hold.

We declare an array of integers called sum. We declare an integer, and we initialize it to 0.

The code of the program

#include <iostream>using namespace std;int main() {int sum[]{154, 576, 218, 943, 472, 661, 333, 752, 599};int counter{0};while(counter<6){cout<<sum[counter]<<endl;counter++;}return 0;}

The output of the program

array C++ while loop

Example3: Prompt the user and validate(negative number)

Introduce the program

Ask the user to enter an integer that satisfies the condition that it is a negative temperature between -1 and -30, then validate the entry.

The code of the program

#include <iostream>using namespace std;int main() {int Temperature{};cout<<“Enter a negative temperature :”;cin>>Temperature;cout<<endl;while(Temperature<0){cout<<Temperature<<” “;++Temperature;}return 0;}

The output of the program

validate negative number

Example4: Prompt the user and validate(positive number)

Introduce the program

Ask the user to enter an integer that satisfies the condition that it is a positive temperature between then validate the entry.

The code of the program

#include <iostream>using namespace std;int main() {int Temperature{};cout<<“Enter a positive temperature :”;cin>>Temperature;cout<<endl;while(Temperature>0){cout<<Temperature<<” “;–Temperature;}return 0;}

The output of the program

validate positive number

Example5: Prompt the user and validate(positive number) version 2 

Introduce the program

Ask the user to enter an integer that satisfies the condition that it is a positive temperature between then validate the entry.

The code of the program

#include <iostream>using namespace std;int main (){int temperature{};int{};cout<<“Enter a positive temperature: “;cin>>temperature;int counter{1};while(temperature>=counter){cout<<counter<<” “;counter++;}cout<<endl;return 0;}

The output of the program

validate positive number  version 2

Conclusion

C++ while loop is an important programming construct. It is a loop that will continue to run as long as the condition it tests for is true. The condition can be any boolean expression, including the logical expressions we have seen.

Pin It on Pinterest

Share This