0
0
C Sharp (C#)programming~10 mins

Array iteration with for and foreach in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array iteration with for and foreach
Start
Choose iteration type
Initialize index i=0
Check i < array.Length?
NoEnd
Access array[i
Process element
Increment i
Back to Check
Start at first element
Get current element
Process element
Move to next element
More elements?
NoEnd
Back to Check
This flow shows how a for loop uses an index to access array elements step-by-step, while foreach directly accesses each element one by one until all are processed.
Execution Sample
C Sharp (C#)
string[] fruits = {"apple", "banana", "cherry"};
for (int i = 0; i < fruits.Length; i++)
{
    Console.WriteLine(fruits[i]);
}

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}
This code prints each fruit in the array twice: first using a for loop with an index, then using a foreach loop that directly accesses each element.
Execution Table
StepLoop TypeIndex or ElementConditionActionOutput
1fori=00 < 3 (true)Print fruits[0] = "apple"apple
2fori=11 < 3 (true)Print fruits[1] = "banana"banana
3fori=22 < 3 (true)Print fruits[2] = "cherry"cherry
4fori=33 < 3 (false)Exit for loop
5foreachfruit="apple"More elements? YesPrint fruitapple
6foreachfruit="banana"More elements? YesPrint fruitbanana
7foreachfruit="cherry"More elements? YesPrint fruitcherry
8foreachEndMore elements? NoExit foreach loop
💡 for loop ends when i reaches array length; foreach ends when no more elements
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
iundefined01233333
fruitundefinedundefinedundefinedundefinedundefined"apple""banana""cherry"undefined
Key Moments - 3 Insights
Why does the for loop use an index variable 'i' but foreach does not?
The for loop uses 'i' to access elements by position (see execution_table rows 1-4). Foreach automatically accesses each element directly without needing an index (rows 5-8).
What happens when the for loop index 'i' equals the array length?
When 'i' equals the array length (3), the condition 'i < fruits.Length' becomes false (row 4), so the loop stops to avoid accessing outside the array.
Can you modify the array elements inside foreach?
No, foreach provides a read-only copy of each element (like in rows 5-7). To modify elements, use a for loop with an index.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'i' at Step 3?
A2
B3
C1
D0
💡 Hint
Check the 'Index or Element' column for Step 3 in the execution_table.
At which step does the foreach loop stop iterating?
AStep 6
BStep 7
CStep 8
DStep 4
💡 Hint
Look for the step where 'More elements? No' appears in the Condition column for foreach.
If the array had 4 elements instead of 3, how many times would the for loop run?
A3 times
B4 times
C5 times
DDepends on the elements
💡 Hint
The for loop runs while i < array.Length; see how length controls iterations in execution_table rows 1-4.
Concept Snapshot
for loop: use index i from 0 to array.Length-1 to access elements
foreach loop: directly access each element without index
for allows modifying elements; foreach is read-only
both loop through all elements in order
for needs condition check; foreach ends when no more elements
Full Transcript
This visual execution shows how to loop through an array in C# using for and foreach. The for loop uses an index variable i starting at 0 and runs while i is less than the array length. Each iteration prints the element at fruits[i]. When i reaches the length, the loop stops to avoid errors. The foreach loop automatically accesses each element one by one, printing each fruit until no elements remain. Variables i and fruit change as the loops run. Key points include that for loops use an index and can modify elements, while foreach loops provide read-only access. The execution table tracks each step, showing conditions, actions, and outputs. This helps beginners see exactly how the loops work step-by-step.