The use of for loop, while loops, and do while loops are clearly discussed in the previous tutorial. As we discussed in the previous tutorial we will use for loop in the C++ when the number of iterations is known ahead of time. When the number of iterations is unknown but the loop termination condition is known, we will use while loops in C++. If the code needs to be executed at least once, such as in menu-driven programs, then we will use a do while loop.
In this tutorial let us discuss the more advanced looping techniques in C++. The more advanced looping techniques in C++ are :
The range-based for loop in the C++ programming language will be covered in this topic. In C++11 and later versions, the C++ language introduced a new concept of the range-based for loop, which is far better compared to the regular For loop. A range-based for loop does not necessarily require a large coding in order to implement for loop iteration. It is a sequential iterator that iterated over each element of the container ( right from the beginning to the end).
Syntax
for (range_declaration : range_expression )
{
loop statement
}
For example,
// initialize an int array
int num_array[3] = {1, 2, 3};
// use of ranged for loop
for (int i: num_array) {
// code
}
In the above example,
int i
Note: Suppose if we don't know the data type of the container elements, we can use the auto keyword, which determines the data type of the range expression automatically.
Let's take a look at a C++ range-based for loop example that prints an int and double array
#include <iostream>
using namespace std;
int main ()
{
int int_array[5] = { 1, 2, 3, 4, 5};
double double_array [5] = { 4.2, 5.7, 4.8, 8.5, 4.0 };
// use range based for loop
for ( const auto &var : int_array)
{
cout << var << " " ;
}
// use auto keyword to automatically specify the data type of darr container.
for ( const auto &var : double_array)
{
cout << var << " " ;
}
return 0;
}
Output:
100 200 300 400 500 4.2 5.7 4.8 8.5 4.0
Let's take a look at a C++ range-based for loop example that prints vector elements.
A vector is just a dynamically sized array. It contains a nearly infinite number of homogenous items (that is, objects of the same kind) that can be easily accessed by their position within the list. You don't need to explicitly use commands like new int[size] or delete[] my array because the underlying array will automatically adjust in size as needed.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int x; // declare integer variable
// declare vector variable
vector <int> vect = {10, 15 , 20, 25, 30};
// display vector elements
for ( int x : vect)
{
cout << x << " ";
}
return 0;
}
Output:
10 15 20 25 30
When a loop is defined within the body of another loop, it is referred to as a nested for loop. Similarly, defining a range in a loop within another range-based loop is known as a nested range-based for loop.
Syntax:
for ( int x : range_expression) // outer loop
{
for ( int y : range_expression) // inner loop
{
// statement to be executed
}
// statement to be executed
}
We define one range-based for loop inside another loop in the above syntax. In C++, this is referred to as an inner and outer range-based for loop.
#include <iostream> using namespace std; int main () { int arr1[5] = { 0, 1, 2, 3,4 }; int arr2[6] = { 1, 2, 3, 4, 5,6 }; // use nested range based for loop for ( int x : arr1 ) { // declare nested loop for ( int y : arr2 ) { cout << " x = " << x << " and j = " << y << endl; } } return 0; }
Output:
x = 0 and j = 1 x = 0 and j = 2 x = 0 and j = 3 x = 0 and j = 4 x = 0 and j = 5 x = 0 and j = 6 x = 1 and j = 1 x = 1 and j = 2 x = 1 and j = 3 x = 1 and j = 4 x = 1 and j = 5 x = 1 and j = 6 x = 2 and j = 1 x = 2 and j = 2 x = 2 and j = 3 x = 2 and j = 4 x = 2 and j = 5 x = 2 and j = 6 x = 3 and j = 1 x = 3 and j = 2 x = 3 and j = 3 x = 3 and j = 4 x = 3 and j = 5 x = 3 and j = 6 x = 4 and j = 1 x = 4 and j = 2 x = 4 and j = 3 x = 4 and j = 4 x = 4 and j = 5 x = 4 and j = 6
A traditional for loop is used to execute the block of code repeatedly until the specified condition is met. A traditional for loop has three parameters: variable initialization, condition specification, and a counter that is incremented by one if the condition remains true. On the other hand, C++ 11 and later versions include a new range-based for loop. There are two parameters: range declaration and range_ expression. It is also used to execute a block of code over and over again.