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

Array iteration with for and foreach in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
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.0
Create an integer variable called length that stores the length of the prices array
Use a for loop with variable i to iterate over prices using length
Use a foreach loop with variable price to iterate over prices
Print 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
1
Create the array of prices
Create an array called prices with these exact values: 10.5, 20.0, 15.75, 30.0.
C Sharp (C#)
Need a hint?

Use double[] prices = {10.5, 20.0, 15.75, 30.0}; to create the array.

2
Create a variable for the array length
Create an integer variable called length that stores the length of the prices array using prices.Length.
C Sharp (C#)
Need a hint?

Use int length = prices.Length; to get the number of items in the array.

3
Use a for loop to print each price
Use a for loop with variable i from 0 to length - 1 to print each price from the prices array using Console.WriteLine(prices[i]).
C Sharp (C#)
Need a hint?

Use for (int i = 0; i < length; i++) and print prices[i] inside the loop.

4
Use a foreach loop to print each price
Use a foreach loop with variable price to iterate over prices and print each price using Console.WriteLine(price).
C Sharp (C#)
Need a hint?

Use foreach (double price in prices) and print price inside the loop.