Mahmoud Derbi pfp
Mahmoud Derbi

@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 ⚑
1 reply
2 recasts
3 reactions