The old classics meet modern convenience β range-based for is the star of the show
The Loop Family
Loops are the workhorses of programming β they repeat a block of code until a condition is met. C++ inherits all three classic loop forms from C (for, while, do-while) and adds a modern superstar: the range-based for loop.
If the classic for loop is a manual transmission β powerful but requires you to manage every gear shift β the range-based for is automatic transmission. Same destination, less fiddling.
The Classic for Loop
#include <iostream>
using namespace std;
int main(){
// Classic for β you control init, condition, and increment
while checks the condition before each iteration β if the condition is false from the start, the body never runs. do-while checks after β the body always runs at least once.
do-while is perfect for menus: you always want to show the menu at least once before asking if the user wants to continue.
while and do-while
#include <iostream>
using namespace std;
int main(){
// while β might not execute at all
int fuel =3;
cout <<"Driving..."<< endl;
while(fuel >0){
cout <<" Fuel left: "<< fuel << endl;
fuel--;
}
cout <<"Out of gas!"<< endl;
cout << endl;
// do-while β always executes at least once (great for menus)
int choice;
do{
cout <<"--- Menu ---"<< endl;
cout <<"1. Play"<< endl;
cout <<"2. Settings"<< endl;
cout <<"3. Quit"<< endl;
choice =3;// simulate user picking Quit
cout <<"You chose: "<< choice << endl;
}while(choice !=3);
cout <<"Goodbye!"<< endl;
return0;
}
Output
Driving...
Fuel left: 3
Fuel left: 2
Fuel left: 1
Out of gas!
--- Menu ---
1. Play
2. Settings
3. Quit
You chose: 3
Goodbye!
Range-Based for β The Star of the Show
Introduced in C++11, the range-based for loop is the modern way to iterate over collections. No index management, no off-by-one errors, no iterator boilerplate. Just tell C++ what to loop over, and it handles the rest.
The syntax is: for (auto& element : collection)
Think of it as saying: "for each element in this collection, do something."
Range-Based for with Vectors
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
vector<int> scores ={95,87,92,78,100};
// Range-based for β clean and readable
cout <<"Scores: ";
for(const auto& score : scores){
cout << score <<" ";
}
cout << endl;
// Modifying elements β use auto& (without const)
Note: Always use & in range-based for: for (auto& x : vec). Without &, you get a copy of each element β modifications won't affect the original, and you pay a performance cost copying every element. Use const auto& when you just need to read, and auto& when you need to modify.
C++17: Structured Bindings in Loops
When iterating over a map, each element is a pair with .first (key) and .second (value). Before C++17, this was clunky. With structured bindings, you can unpack the pair right in the loop header β naming the key and value whatever you want.
Structured Bindings with Maps (C++17)
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
map<string, int> ages ={
{"Alice",30},
{"Bob",25},
{"Carol",28}
};
// Before C++17 β clunky .first and .second
cout <<"Old style:"<< endl;
for(const auto& pair : ages){
cout <<" "<< pair.first<<" is "<< pair.second<< endl;
}
// C++17 structured bindings β clean and readable!
cout <<"\nC++17 style:"<< endl;
for(const auto&[name, age]: ages){
cout <<" "<< name <<" is "<< age << endl;
}
// Modifying values with structured bindings
for(auto&[name, age]: ages){
age++;// happy birthday everyone!
}
cout <<"\nAfter birthdays:"<< endl;
for(const auto&[name, age]: ages){
cout <<" "<< name <<" is now "<< age << endl;
}
return0;
}
Output
Old style:
Alice is 30
Bob is 25
Carol is 28
C++17 style:
Alice is 30
Bob is 25
Carol is 28
After birthdays:
Alice is now 31
Bob is now 26
Carol is now 29
break and continue β Loop Control
break exits the loop entirely β like pulling the emergency stop on a conveyor belt. continue skips the rest of the current iteration and jumps to the next one β like tossing a defective item off the belt and keeping the line moving.