Introduction
The for-loop is the most common looping construct in C++. The for loop iterates over a range of values and executes a code block once per iteration.A loop can be executed one or more times using a specific syntax.In the article, C++ for loop easy guide, we will explain the variation of C++ for loop using many possible cases. We will propose multiple examples to understand the concepts.
What are looping and iteration?
Looping and iteration are programming processes that repeat instructions several times or until some condition is met.
Looping is the third fundamental building block of programming. The programmer utilizes looping to solve so many complex programming problems.
By becoming capable of using repetition, I consider that the programmer puts a firm leg on the path to creating robust programs.
Looping is the procedure of repeating a particular task over and over again.
The specific possibilities in which repetition occurs
- Run the code inside the loop a specific number of times.
- Run the loop while the condition stays true.
- Run the loop until a precise condition evolves false.
- Run the loop forever in if we want.
When we want to display numbers from 0 to 20, we require that the repetition starts from the number 0, and the condition remains true until the count reaches 21 so that the condition becomes invalid.
The condition is not fulfilled, and the counting stops. Each validation to display a new iteration refers to a condition that must be achievable and true.
Looping structure in C++
In C++, loops are used to execute a set of instructions repeatedly. The looping types can be executed in three ways: while loop, do-while loop, and for loop.
Looping structure in C++
A while-loop executes the instructions within its body as long as a given condition is true.
A do-while-loop executes the instructions within its body at least once before testing the condition.
A for-loop executes the instructions within its body for a specific number of iterations or until a given condition becomes false.
C++ for loop
The for loop is the most common way to iterate through a list of data in C++. It is a loop that executes a set of instructions repeatedly until the given condition is met.
for loop
C++ for loop
The for loop consists of three parts: initialization, condition, and increment.
- The statement’s initialization part defines the counter variable’s initial value.
- The conditional statement allows the program to decide and execute one set of instructions if the condition is true or if the condition is false. The first set is to continue looping, and the second set is to stop looping.
- The increment is an integer that starts at the beginning of the sequence and counts up to the end.
- An increment by one would be written as “++i.” An increment by ten would be written as “i+=10”. The increment can be up or down.
- List Element
1
Note
Note that i++ and ++i have the same meaning.
The for loop is a control flow statement in many programming languages. It allows specifying a block or sequence of statements that should be repeated.
A typical use of the for loop is to create a sequence of numbers and print them out.
2
The syntax typically looks like this
for (initialization; condition; increment) {statements}
A for loop may have multiple nested loops and can have an optional else clause at the end.
Example 1
A basic c++ for loop that counts from 0 to 12
The code of the program
#include<iostream>using namespace std;int main() {for( int i{0}; i<=12 ; ++i)cout<<i<<endl;return 0;}
The output of the program
Example 2: the comma operator
The comma operator is used in the following way:for( ; ; )
The code of the program
#include<iostream>using namespace std;int main() {cout<<” “<<endl;for( ; ; )cout<<” Endless loop”<<endl;}
The output of the program
Example 3: for loop array
C++ for loop array works.
The code of the program
#include<iostream>using namespace std;int main() {int Prime_numbers[] {2,3,5,7,11,13,17,19,23,29,31};for (int counter {0}; counter < 6; ++counter) {cout << Prime_numbers[counter] << endl;}
The output of the program
Example 4: Another method to increment the counter
There are many methods to increment the counter of the C++ for loop.
The code of the program
#include<iostream>using namespace std;int main() {for(int counter {10}; counter<150; counter+=15){cout<<counter<<endl; }return 0;}
The output of the program
Example 5 C++ for loop with a double counter
We create a C++ for loop with a double counter to do the multiplication table of specific numbers.
The code of the program
#include<iostream>using namespace std;int main() {for(int a {0},b{1};a <=9; ++a,++b)cout<<a<< “*”<<b<< “=”<< a*b<<endl;cout<<endl;return 0;}
The output of the program
Example 6: C++ for loop vector
The c++ for loop vector works like the example below.
The code of the program
#include<iostream>#include<vector>using namespace std;int main() {vector <int> multiples_of_three {3, 6, 9, 12, 15, 18, 21, 24, 27, 30};for( unsigned counter{0};counter < multiples_of_three.size();++counter)cout<< multiples_of_three[counter]<<endl;cout<<endl;return 0;}
The output of the program
Conclusion
The C++ for loop is the most potent block code in the C++ programming language, so any programmer needs to know how to use it very well to build robust C++ programs.