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

Why loops are needed in C Sharp (C#) - See It in Action

Choose your learning style9 modes available
Why loops are needed
📖 Scenario: Imagine you have a list of 5 friends and you want to say "Hello" to each one. Writing the same greeting again and again is tiring and takes a lot of space. Loops help us repeat actions easily without writing the same code many times.
🎯 Goal: You will create a list of friend names, set up a counter, use a loop to greet each friend, and finally print all greetings.
📋 What You'll Learn
Create a string array called friends with these exact names: "Anna", "Ben", "Cara", "David", "Eva"
Create an integer variable called count and set it to 0
Use a while loop with the condition count < friends.Length to print greetings
Inside the loop, print "Hello, {friends[count]}!" using Console.WriteLine
Increase count by 1 inside the loop
💡 Why This Matters
🌍 Real World
Loops are used in many programs to repeat tasks like processing lists of data, showing menus, or handling user input multiple times.
💼 Career
Understanding loops is essential for any programming job because they help automate repetitive tasks efficiently.
Progress0 / 4 steps
1
Create the list of friends
Create a string array called friends with these exact names: "Anna", "Ben", "Cara", "David", "Eva"
C Sharp (C#)
Need a hint?

Use string[] friends = { "Anna", "Ben", "Cara", "David", "Eva" }; to create the array.

2
Create a counter variable
Create an integer variable called count and set it to 0
C Sharp (C#)
Need a hint?

Use int count = 0; to create the counter.

3
Use a while loop to greet friends
Use a while loop with the condition count < friends.Length to print greetings. Inside the loop, print "Hello, {friends[count]}!" using Console.WriteLine. Increase count by 1 inside the loop.
C Sharp (C#)
Need a hint?

Use while (count < friends.Length) and inside the loop print the greeting and increase count by 1.

4
Run the program to see greetings
Run the program to print all greetings. Use Console.WriteLine inside the loop as before. The output should show greetings for all friends.
C Sharp (C#)
Need a hint?

Make sure your loop prints each greeting on its own line.