@mmderbi
π C++ Programming β Part 14: Loops with Strings & Arrays π
Now that you know how strings and arrays work β letβs learn how to loop through them efficiently π
πΉ Looping Through an Array:
Use a for loop to go through each element one by one π
int numbers[] = {10, 20, 30, 40};
for (int i = 0; i < 4; i++) {
cout << numbers[i] << " ";
}
β
Output: 10 20 30 40
---
πΉ Looping Through a String:
Each character in a string can be accessed like an array element π
string word = "Hello";
for (int i = 0; i < word.length(); i++) {
cout << word[i] << " ";
}
β
Output: H e l l o
---
πΉ Using Range-Based For Loop (Modern C++):
A cleaner and more readable way π
for (char c : word) {
cout << c << " ";
}
β
Same result β but shorter and safer!
---
πΉ Coming next (Part 15):
Weβll explore arrays & memory together β how theyβre stored and how pointers interact with them β‘