DevAcademia lesson/C++ Basics
Arrays and Loops
Topic progressTopic 53 of 109 - 49% complete
~7 min lesson14 learning blocks3 code examples1 checks/interactions
Lesson outline
Traditional For Loop
A common way to iterate over arrays is with a counter-based for loop.
Range-Based For Loop (C++11)
Since C++11, a range-based for loop provides a cleaner and safer way to traverse arrays.
While Loop with Arrays
While loops can also be used with arrays, especially when stopping conditions depend on the data.
Common Loop Patterns
| Pattern | Example |
|---|---|
| Searching | for (int n : arr) if (n == target) { /* found */ } |
| Summation | int sum = 0; for (int n : arr) sum += n; |
| Finding Max | int mx = arr[0]; for (int n : arr) if (n > mx) mx = n; |
| Transformation | for (int &n : arr) n *= 2; |
Was this topic clear?