0
0
Cprogramming~10 mins

Array traversal in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array traversal
Start: i = 0
Check: i < array_length?
NoEnd traversal
Yes
Access array[i
Process element
Increment i
Back to Check
We start from the first element, check if the index is within bounds, access and process the element, then move to the next index until all elements are visited.
Execution Sample
C
#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30};
    int n = 3;
    for (int i = 0; i < n; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}
This code prints each element of the array one by one.
Execution Table
IterationiCondition (i < n)ActionOutput
100 < 3 = TruePrint arr[0] = 1010
211 < 3 = TruePrint arr[1] = 2020
322 < 3 = TruePrint arr[2] = 3030
433 < 3 = FalseExit loop
💡 i reaches 3, condition 3 < 3 is False, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
iundefined0123
arr[i]N/A102030N/A
Key Moments - 3 Insights
Why does the loop stop when i equals the array length?
Because the condition i < n becomes false at i = 3 (see execution_table row 4), so the loop exits to avoid accessing outside the array.
What happens if we try to access arr[i] when i is equal to n?
Accessing arr[n] is out of bounds and causes undefined behavior. The loop condition prevents this by stopping before i reaches n (see execution_table).
Why do we start i from 0 instead of 1?
Array indexes in C start at 0, so the first element is arr[0]. Starting at 0 ensures we visit all elements correctly (see variable_tracker for i values).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i during the third iteration?
A1
B2
C3
D0
💡 Hint
Check the 'i' column in execution_table row 3.
At which iteration does the loop condition become false?
AIteration 4
BIteration 3
CIteration 2
DIteration 1
💡 Hint
Look at the 'Condition (i < n)' column in execution_table row 4.
If the array length n was 4 instead of 3, how many iterations would the loop run?
A5
B3
C4
D2
💡 Hint
The loop runs while i < n, so if n=4, i goes from 0 to 3 (4 iterations).
Concept Snapshot
Array traversal in C:
- Use a for loop: for(int i=0; i<n; i++)
- Start i at 0 (first index)
- Continue while i < n (array length)
- Access elements as arr[i]
- Stops before i reaches n to avoid out-of-bounds
Full Transcript
Array traversal means visiting each element of an array one by one. We start with an index i at 0, the first element. We check if i is less than the array length n. If yes, we access arr[i] and do something with it, like print it. Then we increase i by 1 and repeat. When i equals n, the condition fails and the loop stops. This prevents accessing outside the array, which is unsafe. The example code prints each element of the array. The execution table shows each step: the index i, the condition check, the action, and the output. The variable tracker shows how i and arr[i] change over time. Common confusions include why the loop stops at i = n, why we start at 0, and what happens if we access arr[n]. The quiz questions help check understanding of these steps.