![]() |
| Loops in C++ with Examples | For, While, and Do-While Loop |
📑 Table of Contents ▼
Loop Definition in Programming
C++ Loops: Loop is very important part of every programming languages like C, C++, Java, Python and more computer programming languages. It is used to perform looping operation. When the condition will become false the loop execution will be stopped. Loop in C++ programming can be used to execute a block of code multiple times until a certain condition is meet. In other word we can say that the body continuously until a required condition is fulfill is called looping. I can explain every loops in this article with example simple and easy way.
Types of Loops in C Plus Plus
- While Loop in C++
- Do While Loop in C++
- For Loop in C ++
- Nested Loop in C++
C++ While Loop Definition
While Loop: While loop before value initialization starting value and the given condition. If you are not follow while loop syntax your loop statement are infinite executed. A while loop in c++ programming language is used to execute a block of code as long as the specified condition is true.
Syntax of While Loop in C++
{ body Statements; increment/decrement; } |
Explain While Loop Syntax
while(n<=10) { cout<<"Number is:"<<n<<endl; i++; } |
Example of While Loop in C++ Program
| #include<iostream> int main(){ int i=1; while(i<=5) { cout<<"Your Number is:"<<i<<endl; i++; } } ============OUTPUT============ Your Number is:1 Your Number is:2 Your Number is:3 Your Number is:4 Your Number is:5 |
Print the Table of Any Number Using While Loop
int main(){ int n,i=1; cout<<"Enter Any Number:"; cin>>n; while(i<=10) { cout<<"Table is:"<<n<<"X"<<i<<"="<<i*n<<endl; i++; } } ============OUTPUT============ Enter Any number:6 Table is:6 X 1=6 Table is:6 X 2=12 Table is:6 X 3=18 Table is:6 X 4=24 Table is:6 X 5=30 Table is:6 X 6=36 Table is:6 X 7=42 Table is:6 X 8=48 Table is:6 X 9=54 Table is:6 X 10=60 |
Definition of Do While Loop in C++
Do-While Loop: The do-while loop in C++ is used to repeatedly execute a block of code execute at least one time and then continue executing it as long as a specified condition remains true. do-while loop is used to execute a block of code as long as the specified condition is true.
Syntax of Do While Loop in C++
{ body Statements; increment/decrement; while(condition); } |
Explain Do-While Loop Syntax
do { cout<<"Number is:"<<n<<endl; i++; while(i<=10); } |
Example of Do While Loop in C++ Program
int main(){ int i=1; do { cout<<"Your Number is:"<<i<<endl; i++; } while(i>=5); } ============OUTPUT============ Your Number is:1 Note: Here condition is false but do-while loop at least one time execute. |
Example of Do While Loop in C++
int main(){ int i=1; do { cout<<"Your Number is:"<<i<<endl; i++; } while(i<=5); } ============OUTPUT============ Your Number is:1 Your Number is:2 Your Number is:3 Your Number is:4 Your Number is:5 |
Print the Table of Any Number Using Do While Loop
int main(){ int n,i=1; cout<<"Enter Any Number:"; cin>>n; do { cout<<"Table is:"<<n<<"X"<<i<<"="<<i*n<<endl; i++; } while(i<=10); } ============OUTPUT============ Enter Any number:6 Table is:6 X 1=6 Table is:6 X 2=12 Table is:6 X 3=18 Table is:6 X 4=24 Table is:6 X 5=30 Table is:6 X 6=36 Table is:6 X 7=42 Table is:6 X 8=48 Table is:6 X 9=54 Table is:6 X 10=60 |
For Loop in C++ Definition
For Loop: A for loop in C++ language is used to execute a block of code a specific number of times. For loop there are three main part in c like initialization, condition and increment /decrement. initialization part executes only one at a time. A for loop in c++ programming language is used to execute a block of code as long as the specified condition is true.
Syntax of For Loop in C++
{ body Statements; } |
Explain For Loop Syntax
{ cout<<"Number is:"<<i<<endl; } |
Example of For Loop in C++ Program
int main(){ for(int i=1;i<=5;i++) { cout<<"Number is:"<<i<<endl; } } ============OUTPUT============ Your Number is:1 Your Number is:2 Your Number is:3 Your Number is:4 Your Number is:5 |
Write a Program to Print Table of a Number Using For Loop
int main(){ int n; cout<<"Enter Any Number:"; cin>>n; for(int i=1;i<=10;i++) { cout<<"Table is:"<<n<<"X"<<i<<"="<<i*n<<endl; } getch(); } ============OUTPUT============ Enter Any number:6 Table is:6 X 1=6 Table is:6 X 2=12 Table is:6 X 3=18 Table is:6 X 4=24 Table is:6 X 5=30 Table is:6 X 6=36 Table is:6 X 7=42 Table is:6 X 8=48 Table is:6 X 9=54 Table is:6 X 10=60 |
Nested Loop in C++ Programming
Nested Loop: A nested loop in C++ is a loop that exists inside another loop. The inner loop is executed completely for each iteration of the outer loop. Nested loops are commonly used for iterating over multi-dimensional data structures like arrays or matrices.
Syntax of Nested Loop C++
{ for(initialization;condition;increment/decrement) { inner body Statements; } outer body Statements; } |
Examples of Nested Loop in C++ Programming
int main(){ int n,i,j; cout<<"Enter Any Number:"; cin>>n; for(i=2;i<n;i++) { int x=i,y=0; for(j=2;j<=x-1;j++) { if(x%j==0) { y=1; } } if(y==0) cout<<" "<<x; } } ============OUTPUT============ Enter Any Number:20 2 3 5 7 11 13 17 19 |
Conclusion of Loops in C++ Language
Loop Conclusion: A loops in a main part of every programming languages because loop concept are perform different kind operation like fetch data, print multiple information and more operations. I can explain every loop in this article if your any question or problems than you can contact me by email. I can every solution loop related in next coming soon article.

