Lesson 5 of 19
Loops
Loops in C++
C++ has all the loops from C — for, while, do-while — plus a powerful C++11 addition: the range-based for loop.
Traditional for Loop
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
while Loop
int n = 1;
while (n <= 10) {
cout << n << " ";
n *= 2;
}
cout << endl;
// 1 2 4 8
Range-Based for Loop
C++11 introduced a cleaner syntax for iterating over collections:
#include <vector>
using namespace std;
vector<int> nums = {10, 20, 30, 40};
for (int n : nums) {
cout << n << endl;
}
Use const string& for strings to avoid copying:
vector<string> names = {"Alice", "Bob", "Carol"};
for (const string& name : names) {
cout << "Hello, " << name << endl;
}
break and continue
break exits the loop; continue skips to the next iteration:
for (int i = 0; i < 10; i++) {
if (i == 3) continue; // skip 3
if (i == 7) break; // stop at 7
cout << i << " ";
}
// 0 1 2 4 5 6
Nested Loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << i * j << " ";
}
cout << endl;
}
Your Task
- Use a
forloop to print1 2 3 4 5on one line (space-separated, no trailing space) - Use a range-based for loop to print each language in
{"C", "C++", "Go"}on its own line
JSCPP loading...
Loading...
Click "Run" to execute your code.