In this C++ article we are going to talk about Loops Complete Example in C++, we are going to learn different C++ Loops like For Loop, While Loop and Do While Loop. There may be a situation, when you need to execute a block of code several number of times, using loops you can do these kind of functionalities. A loop statement allows us to execute a statement or group of statements multiple times. There are different types of loops that you can use in c++, for loop, while loop, do while loop, also there are different control statements like break and continue in c++ loops, using control statements we can change the execution of loops from its normal sequence.
Note: you can watch the complete video for this article(C++ Loops Complete Example ) at the end.
Also you can check more articles on c++ programming language
1: C++ Introduction & Program Structure
2: C++ Variables And Data Types
1: For Loop
So now this is the code for this loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<iostream> using namespace std; int main() { for (int i = 1; i < 10; i++) { cout << "Value of i is : " << i << endl; } cout << "Condition is false" << endl; return 0; } |
int i, this is the initial step of the loop and this step will be executed first and only once.
i < 10, this is the condition for the loop, if it is true the body of the loop is executed, if it is false body of the loop
does not execute and flow of the control jumps.
i++, in here we need to increment the value according to the loop condition.
If you execute the code this will be the result.
1 2 3 4 5 6 7 8 9 10 11 |
Value of i is : 1 Value of i is : 2 Value of i is : 3 Value of i is : 4 Value of i is : 5 Value of i is : 6 Value of i is : 7 Value of i is : 8 Value of i is : 9 Condition is false Press any key to continue . . . |
2: While Loop
This is the simplest type of loops in c++, it will repeats statement while expression is true, and if the expression is no
longer true, the loop ends, and the program continues after that loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<iostream> using namespace std; int main() { int a = 10; while (a < 30) { cout << "The value of a is : " << a << endl; a++; } cout << "Out of the loop" << endl; return 0; } |
If you run the code this will be the result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
The value of a is : 10 The value of a is : 11 The value of a is : 12 The value of a is : 13 The value of a is : 14 The value of a is : 15 The value of a is : 16 The value of a is : 17 The value of a is : 18 The value of a is : 19 The value of a is : 20 The value of a is : 21 The value of a is : 22 The value of a is : 23 The value of a is : 24 The value of a is : 25 The value of a is : 26 The value of a is : 27 The value of a is : 28 The value of a is : 29 Out of the loop Press any key to continue . . . |
3: Do While Loop
Do while loop is like while and for loop, but with the change that do while loop tests the condition at the bottom of the loop. and do while loop is guaranteed to execute at least one time. now in the for and while loop the condition is tested at the top of the code, but in do while loop it is done in the bottoms of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int main() { int a = 1; do { cout << "value of a: " << a << endl; a++; } while (a < 10); return 0; } |
If you execute the code this will be the result
1 2 3 4 5 6 7 8 9 10 |
value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 Press any key to continue . . . |
Break Statement
When we use break statement in a loop, the loop is immediately terminated and program control resumes at the next statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<iostream> using namespace std; int main() { for (int i = 1; i < 20; i++) { cout << "Value of i is : " << i << endl; if (i == 12) { break; } } cout << "Out of the loop " << endl; return 0; } |
If you execute the code this will be the result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Value of i is : 1 Value of i is : 2 Value of i is : 3 Value of i is : 4 Value of i is : 5 Value of i is : 6 Value of i is : 7 Value of i is : 8 Value of i is : 9 Value of i is : 10 Value of i is : 11 Value of i is : 12 Out of the loop Press any key to continue . . . |
Break statement with do while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int a = 1; do { cout << " Value of a is : " << a << endl; a++; if (a > 10) { break; cout << "Breaking the loop" << endl; } } while (a < 20); |
Execute the code this will be the result
1 2 3 4 5 6 7 8 9 10 11 |
Value of a is : 1 Value of a is : 2 Value of a is : 3 Value of a is : 4 Value of a is : 5 Value of a is : 6 Value of a is : 7 Value of a is : 8 Value of a is : 9 Value of a is : 10 Press any key to continue . . . |
Continue Statement
Continue statement works like break statement, but instead of terminating the loop, it continues to the next iteration of the loop and skip any code that is in the between.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<iostream> using namespace std; int main() { int a = 1; do { if (a == 10) { //skip iteration a++; continue; } cout << "Value of a is : " << a << endl; a++; } while (a < 20); return 0; } |
Run the code and this is the result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Value of a is : 1 Value of a is : 2 Value of a is : 3 Value of a is : 4 Value of a is : 5 Value of a is : 6 Value of a is : 7 Value of a is : 8 Value of a is : 9 Value of a is : 11 Value of a is : 12 Value of a is : 13 Value of a is : 14 Value of a is : 15 Value of a is : 16 Value of a is : 17 Value of a is : 18 Value of a is : 19 Press any key to continue . . . |
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email