Introduction
Programmers aspire to develop programming languages so that the code is short, easy to use, and can solve many problems. From this standpoint, range based for loop c++ is a new looping construct in C++11. The article explains the range-based for loop: definition, the syntax to use, and some examples.
Types of Loops
The following are the four types of loops that you can use in your programs:
- While-loop: A while-loop is a type of loop that has the condition at the top of the loop and typically executes a statement or block multiple times.
- Do-while Loop: A do-while loop is similar to a while loop, but it executes the statements at least once before checking the condition.
- For Loop: A for loop is a type of loop that allows you to iterate through a sequence and execute statements for each item in that sequence, such as numbers or letters.
- Nested Loop: A nested loop is when one or more loops are inside another one, also known as an innermost loop or outermost loop.
What’s new about the C++11
What’s new about the C++11
C++11 is a C++ programming language standard published in 2011. It has new features that make it easier to use and fixes some of the problems found in earlier versions of the standard.The new features in C++11 are:
– Automatic type deduction for local variables – Generalized constant expressions – Lambda expressions – Variadic templates – Extended constexpr
What is a range based for loop c++?
The range-based for loop is an improvement over the traditional C++ for loop. It is more powerful and flexible because it lets you step through a sequence of values without using an index.
The range-based for loop is a new looping construct in the C++11 standard that generalizes the traditional for loop.
Range based for loop C++ syntax
The range base for loop is introduced in C++ 11. The range base for loop has the following structure.
for(variable_type variable_name: sequence)statement;for(variable_type variable_name: sequence){statement,}
Range based for loop C++ examples
Example 1
Introduce the program
Declare an integer array. Pupils_ages is the name of the array. We initialize the array to12,11,11,12,10,9,13
The code of the program
#include <iostream>using namespace std;int main() {int pupils [] {12,11,11,12,10,9,13};for(int classes : pupils)cout<<classes<<endl;}
The output of the program
Example 2
Introduce the program
We can develop the program and use a keyword that replaces the variable type with another keyword called auto.
The code of the program
#include <iostream>using namespace std;int main() {int pupils [] {12,11,11,12,10,9,13};for(auto classes : pupils)cout<<classes<<endl;}
The output of the program
Example 3
A string is a sequence of characters, which is stored and handled as a single entity. We will use the string in range based for loop in C++. There are two kinds of string in C++. There are two types of string in C++:
1
C style string
C-style strings are a type of data structure in C and C++. They are arrays of characters terminated by the null character.
2
C++ style string
C++ style string is a data type that stores a string in a single variable. It is one of the most widely used data types in the C++ programming language.
A C++-style string can be defined as an array of characters that stores text or a sequence of characters terminated by and including the first null character.
C++ style strings are usually implemented as arrays but can also be implemented using pointers to char.
We will build a C style string for C++ using range based for loop C++.
The code of the program
#include <iostream>#include <vector>using namespace std;int main() {for(auto phrase : “This is a phrase”)cout<<phrase;cout<<endl;}
The output of the program
Example 4
Introduce the program
Declare an integer vector. Cities is the name of the vector. We initialize the vector to12, 31, 35, 13, 88, 38, 11, 22.
The code of the program
#include <iostream>#include <vector>using namespace std;int main() {vector<int> cities = {12, 31, 35, 13, 88, 38, 11, 22};for (auto distances : cities)cout << distances << endl;cout<<endl;}
The output of the program
Example 5
Introduce the program
We will create a range based for loop C++ using string characters.
The code of the program
#include <iostream>#include <vector>using namespace std;int main() {string Letters = “programming”;for (char ch : Letters) cout << ch << ‘ ‘;cout<<endl;}
The output of the program
Conclusion
Range based for loop c++ is simple and easy for looping inside C++ for loop, so I encourage you to build this code block as you can in your pieces of code.