Array iteration with for and foreach
📖 Scenario: You are helping a small shop owner who wants to list the prices of items they sell. They have a list of prices and want to see each price printed one by one.
🎯 Goal: Build a simple C# program that stores item prices in an array and then prints each price using both a for loop and a foreach loop.
📋 What You'll Learn
Create an array called
prices with the exact values: 10.5, 20.0, 15.75, 30.0Create an integer variable called
length that stores the length of the prices arrayUse a
for loop with variable i to iterate over prices using lengthUse a
foreach loop with variable price to iterate over pricesPrint each price inside both loops using
Console.WriteLine(price)💡 Why This Matters
🌍 Real World
Iterating over arrays is common when handling lists of data like prices, names, or scores in many programs.
💼 Career
Knowing how to use <code>for</code> and <code>foreach</code> loops to process arrays is a basic skill needed for software development jobs.
Progress0 / 4 steps