While Loop in C++ Programming Language

Image
  While Loop in C++ Programming Language We have discussed about the entry-controlled loop or pre-tested loop previously in our blog posts. These statements or loops are used to repeat a piece of code for a specific number of times as per our program requirement. We have studied about the for loop already. Today in this blog post we are going to study about the while loop in C++ programming language. And we will look at how this while loop actually works and how we can stop it by making some condition false so that the loop might not run infinitely. We use while loop in C++ program when the number of iterations is not known in advance and depends on the condition. When we know exactly that how many times a loop should run then we use the for loop instead of while loop. Basic Syntax Of While Loop in C++ Programming Language Basic Working Of While Loop In C++ Programming Language Now we are going to discuss about the basic working of while loop in C++ programming....

while loop in c programming language

 while loop in the C programming language



In computer programming languages there is a concept of control flow statements. In control flow statements we specify some conditions and based on those conditions whether they are true or false we run some iterative piece of code if the condition is true and terminate the control statement if the condition is false. 

There are mainly three types of control flow statements that we use in our programs to get the desired result, these three types of control statements are


Three types of Control flow statements

1) do-while loop

2) while loop

3) for loop


In today's post, we will discuss the while loop control statement. while loop is also known as a pre-tested loop in which before running the piece of code the condition is examined first. If the condition is true only then the while loop will execute and if the condition is false then the while loop statement terminates.

In these control statements, the piece of code is iterated according to the given condition. Now let us look at the syntax of the while loop statement. Below is the syntax of the while loop in c programming.


Syntax of while loop in c program

while(condition)

{

// code to be executed 

}


An expression is given in a while loop if the expression is true code repeats multiple times unless that condition becomes false. Now let us look at the simple example of the while loop. Below is the while loop example,





Explanation of the above coding example

In this while loop example program we simply declared an integer variable with constant value 1. After declaring and initializing this variable we are going to use this variable within the while expression and check for a condition that if the variable number has a value less or equal to 10 while(number<=10) than the given lines of code within curly brackets will repeat or iterate until number is less or equal to 10. At the end of this coding block, we are going to increment or add 1 in this number variable with each iteration until the number becomes equal to 10 and at that point, the while loop will terminate.

Congratulations as always, we did it again, we have successfully executed the while loop in C++ program.






Comments

Popular posts from this blog

How to run c project in visual studio community 2022

While Loop in C++ Programming Language

if and if else statement in C++ programming language