This article mainly introduces an in-depth understanding of for loops and range-based for statements in C++. The examples provided are very detailed, offering significant reference value for your study or work. Friends in need can refer to it!
Today, let’s discuss the <span>for</span>
loop and the range-based <span>for</span>
statement in C++. They are like two super handy magic wands that allow us to easily manipulate data in our programs!
1. Traditional For Loop
The traditional <span>for</span>
loop is like a hardworking little bee, following the route we set, step by step, to gather honey (execute operations) in the garden of code. Its basic structure is as follows:
for (initialization; condition; iteration) {
// Loop body, where the little bee gathers honey!
}
For example, if we want the little bee to count from 1 to 10, the code can be written like this:
#include <iostream>
int main() {
for (int i = 1; i <= 10; ++i) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
Here, <span>int i = 1</span>
is the initialization expression, telling the little bee where to start; <span>i <= 10</span>
is the condition expression, like a little threshold, only when this condition is met can the little bee continue to gather honey; <span>++i</span>
is the iteration expression, each time honey is gathered, the little bee moves forward to prepare for the next place to gather honey. Isn’t it very orderly?
We can also use the <span>for</span>
loop to operate on arrays. For example, if we have an array containing the names of fruits:
#include <iostream>
#include <string>
int main() {
std::string fruits[] = {"Apple", "Banana", "Orange", "Strawberry", "Watermelon"};
for (int i = 0; i < 5; ++i) {
std::cout << "I like to eat " << fruits[i] << std::endl;
}
return 0;
}
In this way, the little bee will tell us the names of the fruits in the array one by one. Isn’t it fun?
2. Range-Based For Statement
The range-based <span>for</span>
statement is like a smart little assistant that can automatically help us traverse the elements in a container or array, without us having to carefully set the start and end conditions like in a traditional <span>for</span>
loop.
For example, using the same fruit array, we can write it like this with a range-based <span>for</span>
statement:
#include <iostream>
#include <string>
int main() {
std::string fruits[] = {"Apple", "Banana", "Orange", "Strawberry", "Watermelon"};
for (std::string fruit : fruits) {
std::cout << "I like to eat " << fruit << std::endl;
}
return 0;
}
Look, isn’t it super concise? It’s like we tell the little assistant: “Go bring me all the fruits in the array to see.” Then the little assistant will bring us the fruits one by one without us worrying about how to get them; it knows how to do it by itself.
For the <span>vector</span>
container, it’s the same. If we have a <span>vector</span>
containing numbers:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
Friends, both of these loop methods have their own advantages. The traditional <span>for</span>
loop is suitable when we need precise control over the number of loops and indices, while the range-based <span>for</span>
statement makes traversing containers and arrays much easier and simpler, like having a caring little assistant by your side. Everyone, go try it out and make our programming journey more enjoyable!
Note: For reference only