C++ Loops


A loop is defined as a block of statements, which are repeatedly executed for number times in C++ program.

A sequence of statements will be executed after a specified condition is true. This sequence of statements to be executed will be kept inside the flower brackets { } which are known as the Loop body. After every execution of the loop body, the condition will be verified. If it is true the loop body will be executed again. When it is false the loop body will not be executed again.

C++ loop

Three types of Loops in C++ programming language:

  • while loop
  • do-while loop
  • for loop

C++ while


C++ While loop

The while is an entry-controlled loop statement. The test-condition evaluates if the condition is true, then the body of the loop will be executed. This process of execution of repeated until the test condition becomes false and the control is transferred out of the loop.


Syntax:-

while(condition) {
   statement(s); //body of the loop
}

Example:-

#include    
using namespace std;
  
int main(){     
	int i=1;      
	while(i<=5){      
		cout << "Print the number " << i << endl;      
		i++;      
	}  
	return 0;
} 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.