Bird
Raised Fist0
DSA C#programming~10 mins

Array Traversal Patterns in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Traversal Patterns
Start at index 0
↓
Check: index < array length?
No→End traversal
Yes↓
Access element at index
↓
Process element
↓
Increment index
↩Back to Check
Start from the first element, check if the index is within array bounds, access and process the element, then move to the next index until all elements are visited.
Execution Sample
DSA C#
int[] arr = {10, 20, 30};
for (int i = 0; i < arr.Length; i++)
{
    Console.WriteLine(arr[i]);
}
This code prints each element of the array one by one from start to end.
Execution Table
StepOperationIndex (i)Condition (i < Length)Element AccessedOutputArray State
1Initialize i00 < 3 = Truearr[0] = 10Print 10[10, 20, 30]
2Increment i11 < 3 = Truearr[1] = 20Print 20[10, 20, 30]
3Increment i22 < 3 = Truearr[2] = 30Print 30[10, 20, 30]
4Increment i33 < 3 = FalseNo accessEnd loop[10, 20, 30]
💡 Loop ends when index i reaches 3, which is not less than array length 3.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4 (End)
iundefined0123
Outputempty102030complete
Array[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
Key Moments - 3 Insights
Why does the loop stop when i equals the array length, not when i is greater?
Because the condition i < array length must be true to access elements safely. When i equals length, it is out of bounds, so the loop stops as shown in step 4 of the execution_table.
What happens if we start i at 1 instead of 0?
The loop will skip the first element (index 0) and start from the second element, as the index controls which element is accessed, shown by the index column in the execution_table.
Why do we access arr[i] inside the loop?
Because i changes each iteration, accessing arr[i] lets us process each element in order, as seen in the Element Accessed column of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at Step 3?
A1
B2
C0
D3
💡 Hint
Check the 'Index (i)' column at Step 3 in the execution_table.
At which step does the loop condition become false?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Condition (i < Length)' column to find when it becomes false.
If the array had 4 elements instead of 3, how many steps would the loop run before stopping?
A4
B5
C3
D2
💡 Hint
The loop runs while i < array length; more elements mean more iterations.
Concept Snapshot
Array Traversal Pattern:
- Start index at 0
- Loop while index < array length
- Access element at current index
- Process element
- Increment index
- Repeat until all elements visited
Full Transcript
Array traversal means visiting each element in order from the first to the last. We start at index zero and check if the index is less than the array length. If yes, we access and process the element at that index, then increase the index by one. This repeats until the index reaches the array length, which stops the loop because accessing beyond would cause an error. This pattern is common for reading or processing all elements in an array.

Practice

(1/5)
1. What does the following C# code do?
int[] arr = {1, 2, 3, 4};
for (int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + " ");
}
easy
A. Prints all elements of the array from start to end
B. Prints all elements of the array in reverse order
C. Prints only the first element of the array
D. Causes a runtime error due to wrong loop condition

Solution

  1. Step 1: Understand the loop range

    The loop starts at 0 and runs while i is less than arr.Length (4), so i goes 0,1,2,3.
  2. Step 2: Print elements in order

    Each iteration prints arr[i], so it prints 1 2 3 4 in order.
  3. Final Answer:

    Prints all elements of the array from start to end -> Option A
  4. Quick Check:

    Loop from 0 to length-1 prints all elements [OK]
Hint: Loop from 0 to length-1 prints all elements in order [OK]
Common Mistakes:
  • Thinking loop prints in reverse
  • Confusing loop condition with <= instead of <
  • Assuming only first element prints
2. Which of the following is the correct syntax to traverse an array arr in C# using a foreach loop?
easy
A. foreach (int i in arr) { Console.Write(i); }
B. foreach (arr in int i) { Console.Write(i); }
C. foreach (int arr in i) { Console.Write(i); }
D. foreach (int i; arr) { Console.Write(i); }

Solution

  1. Step 1: Recall foreach syntax

    The correct syntax is: foreach (type variable in collection) { ... }
  2. Step 2: Match syntax to options

    foreach (int i in arr) { Console.Write(i); } matches correct syntax: foreach (int i in arr) { Console.Write(i); }
  3. Final Answer:

    foreach (int i in arr) { Console.Write(i); } -> Option A
  4. Quick Check:

    foreach loops use 'variable in collection' syntax [OK]
Hint: Remember foreach uses 'variable in collection' syntax [OK]
Common Mistakes:
  • Swapping variable and collection order
  • Using semicolon instead of 'in'
  • Incorrect variable declaration inside foreach
3. What is the output of the following code?
int[] arr = {10, 20, 30, 40};
for (int i = arr.Length - 1; i >= 0; i--) {
  Console.Write(arr[i] + " ");
}
medium
A. 40 30 20
B. 10 20 30 40
C. 40 30 20 10
D. Runtime error due to loop condition

Solution

  1. Step 1: Analyze loop start and end

    Loop starts at arr.Length - 1 = 3 and runs while i >= 0, decrementing i each time.
  2. Step 2: Trace printed elements

    Prints arr[3]=40, arr[2]=30, arr[1]=20, arr[0]=10 in that order.
  3. Final Answer:

    40 30 20 10 -> Option C
  4. Quick Check:

    Loop from end to start prints reversed array [OK]
Hint: Loop from length-1 down to 0 prints array in reverse [OK]
Common Mistakes:
  • Confusing loop direction and printing forward
  • Off-by-one errors in loop condition
  • Assuming runtime error due to negative index
4. Find the error in this array traversal code:
int[] arr = {5, 10, 15};
for (int i = 0; i <= arr.Length; i++) {
  Console.Write(arr[i] + " ");
}
medium
A. Array declaration is incorrect
B. Variable i should start from 1, not 0
C. Console.Write should be Console.WriteLine
D. Loop condition should be i < arr.Length, not <= arr.Length

Solution

  1. Step 1: Check loop condition

    Loop runs while i <= arr.Length, but arr indices go from 0 to arr.Length - 1.
  2. Step 2: Identify out-of-range access

    When i == arr.Length (3), arr[3] is accessed causing runtime error (IndexOutOfRangeException).
  3. Final Answer:

    Loop condition should be i < arr.Length, not <= arr.Length -> Option D
  4. Quick Check:

    Array indices go 0 to length-1, use < not <= [OK]
Hint: Use i < arr.Length to avoid out-of-range errors [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Starting loop at 1 instead of 0
  • Confusing Write and WriteLine methods
5. Given an integer array arr, which code snippet correctly prints only the even-indexed elements (0, 2, 4, ...) of the array?
hard
A. foreach (int i in arr) { if (i % 2 == 0) Console.Write(i + " "); }
B. for (int i = 0; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); }
C. for (int i = 1; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); }
D. for (int i = 0; i < arr.Length; i++) { if (arr[i] % 2 == 0) Console.Write(arr[i] + " "); }

Solution

  1. Step 1: Understand requirement

    We want to print elements at even indexes: 0, 2, 4, ...
  2. Step 2: Analyze options

    for (int i = 0; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); } increments i by 2 starting at 0, so it accesses even indexes correctly.
    for (int i = 1; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); } accesses odd indexes.
    foreach (int i in arr) { if (i % 2 == 0) Console.Write(i + " "); } checks element values for evenness, not indexes.
    for (int i = 0; i < arr.Length; i++) { if (arr[i] % 2 == 0) Console.Write(arr[i] + " "); } prints elements with even values, not indexes.
  3. Final Answer:

    for (int i = 0; i < arr.Length; i += 2) { Console.Write(arr[i] + " "); } -> Option B
  4. Quick Check:

    Increment index by 2 from 0 to get even indexes [OK]
Hint: Increment loop index by 2 starting at 0 for even indexes [OK]
Common Mistakes:
  • Confusing even index with even element value
  • Starting loop at 1 for even indexes
  • Using foreach without index tracking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard