There many types of loop in c++ for programmer to use in any situation and business logical such as:
– while loop
– for loop
– do… while loop
– nested loops
Example : WHILE LOOP
#include <iostream> // www.learning2night.com using namespace std; int main(){ // the output will print www.Learning2night.com 5 time int n=5; while(n>0){ n--; cout<<"www.Learning2night.com"<<endl; } }
Example :FOR LOOP
FOR LOOP STYLE1
#include <iostream> // www.learning2night.com using namespace std; int main(){ // the output will print www.Learning2night.com 5 time for(int i=0;i<5;i++){ cout<<"www.Learning2night.com"<<endl; } }
FOR LOOP STYLE 2
#include <iostream> // www.learning2night.com using namespace std; int main(){ // the output will print www.Learning2night.com 5 time int n=5; for(;n>0;n--){ cout<<"www.Learning2night.com"<<endl; } }
Example :DO WHILE
#include <iostream> // www.learning2night.com using namespace std; int main(){ // the output will print www.Learning2night.com 5 time int n=5; do{ n--; cout<<"www.Learning2night.com"<<endl; }while(n>0); }