Step 4: Loops (Repeating Code)

In Step 4, you’ll learn about loops in C#, which allow you to repeat a block of code multiple times. Loops are extremely useful when you want to perform the same action repeatedly without writing the same code over and over.


What is a Loop?

A loop is a control structure that allows you to execute a block of code multiple times, based on a condition. C# provides several types of loops, but the most common ones are:

  1. for loop
  2. while loop
  3. do-while loop

Each loop has its use cases, but they all share the same purpose: running code repeatedly.


1. The for Loop

The for loop runs a block of code a specific number of times.

Basic Structure of a for Loop:

    
if (condition)
for (initialization; condition; update)
{
    // Code to run
}
    
  • initialization: This usually sets up a variable (e.g., int i = 0).
  • condition: The loop runs as long as this condition is true (e.g., i < 5).
  • update: This is usually used to increase or decrease the value of a variable (e.g., i++).

Example:

    
for (int i = 0; i < 5; i++)
{
    Console.WriteLine("This is loop iteration " + i);
}
    

Explanation:

  • This loop starts with i = 0.
  • The loop continues to run as long as i < 5.
  • After each iteration, i increases by 1 (i++).
  • This code prints “This is loop iteration X” five times, where X is the current loop iteration (from 0 to 4).

2. The while Loop

The while loop repeats a block of code as long as a condition is true.

Basic Structure of a while Loop:

    
while (condition)
{
    // Code to run
}
    
  • condition: The loop runs as long as this condition remains true.

Example:

    
int i = 0;

while (i < 5)
{
    Console.WriteLine("This is loop iteration " + i);
    i++; // Don't forget to increase the variable to avoid infinite loops!
}
    

Explanation:

  • This loop starts with i = 0.
  • The loop continues to run as long as i < 5.
  • Inside the loop, i is increased by 1 after every iteration, preventing the loop from running forever.

3. The do-while Loop

The do-while loop is similar to the while loop, but with one key difference: the code block will always run at least once, even if the condition is false from the beginning.

Basic Structure of a do-while Loop:

    
do
{
    // Code to run
}
while (condition);
    

Example:

    
int i = 0;

do
{
    Console.WriteLine("This is loop iteration " + i);
    i++;
} while (i < 5);
    

Explanation:

  • The loop will execute the code block first, then check the condition (i < 5).
  • If the condition is true, the loop repeats. If it’s false, the loop stops.
  • This loop runs exactly the same as the while loop in this case, but the main difference is that the block runs at least once, even if the condition is initially false.

4. Using Loops in a Real-World Scenario

Let’s say you want to ask the user to enter a number and print out that number of stars (*). You can use a loop to repeat the print statement the number of times the user specifies.

Example:

    
using System;

public class Program
{
    public static void Main()
    {
        int number = 5; // Predefined number for testing instead of Console.ReadLine()

        for (int i = 0; i < number; i++)
        {
            Console.Write("*"); // Prints * without a new line
        }

        Console.WriteLine(); // Move to the next line after the loop finishes
    }
}

    

Explanation:

  • The program asks the user for a number.
  • A for loop runs from 0 to the user-specified number and prints a * on each iteration.
  • Once the loop is done, the code moves to the next line.

Summary of Step 4:

  • for loops are great when you know how many times the loop should run.
  • while loops repeat as long as the condition remains true.
  • do-while loops guarantee the code block runs at least once, even if the condition is false.
  • Loops allow you to repeat tasks, like printing something multiple times or processing items in a list.