Loops - potatoscript/csharp GitHub Wiki

πŸ”„ Loops in C# (for, while) πŸ”„

πŸ§‘β€πŸ’» What Are Loops? πŸ§‘β€πŸ’»

Imagine you’re at a party, and your job is to hand out invitations to all the guests. Instead of giving each invitation one by one, you decide to use a loop. A loop is like a machine that repeats an action over and over until it’s done!

In programming, loops are used to repeat code a certain number of times or until a certain condition is met. There are two common types of loops in C#: for loops and while loops.


πŸ§‘β€πŸ’» For Loop πŸ§‘β€πŸ’»

A for loop is like saying, "I will repeat this action a specific number of times." You know exactly how many times you need to repeat something, like handing out invitations to 10 guests.

🏷️ Basic Syntax of a For Loop:

for (int i = 0; i < 10; i++)
{
    // Code to repeat
    Console.WriteLine("Invitation " + (i + 1));
}
  • int i = 0: This is where you start. You begin counting from 0.
  • i < 10: This is the condition. The loop will keep going as long as i is less than 10.
  • i++: This increases i by 1 each time the loop runs, so it will count from 0 to 9 (10 times).

πŸ§‘β€πŸ’» Let’s Make a Fun Example: Handing Out Invitations πŸ§‘β€πŸ’»

Let’s say you’re handing out invitations to a party. You have 10 invitations, and you need to give them to 10 guests. You can use a for loop to repeat the task 10 times.

using System;

class Program
{
    static void Main()
    {
        // For loop to hand out 10 invitations
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Handing out invitation #" + (i + 1) + " to guest!");
        }
    }
}

Explanation:

  • The for loop repeats the action of handing out an invitation 10 times.
  • The number i starts at 0 and increases by 1 with each repetition.
  • The message is printed, showing which invitation number is being handed out.

πŸ§‘β€πŸ’» While Loop πŸ§‘β€πŸ’»

A while loop is a bit different. It’s like saying, "I will keep doing this as long as a certain condition is true." It’s useful when you don’t know how many times the action will need to repeat, but you know the condition you want to check.

🏷️ Basic Syntax of a While Loop:

int i = 0;
while (i < 10)
{
    // Code to repeat
    Console.WriteLine("Invitation " + (i + 1));
    i++; // Increase i to avoid an infinite loop
}
  • int i = 0;: We start with i set to 0.
  • while (i < 10): The condition for the loop to keep running. As long as i is less than 10, it will repeat.
  • i++: Just like the for loop, we increase i by 1 to eventually stop the loop.

πŸ§‘β€πŸ’» Let’s Make Another Fun Example: Handing Out Invitations (While Loop) πŸ§‘β€πŸ’»

Let’s use the while loop to repeat the task of handing out invitations until we’re done. We don’t know exactly how many guests there are, but we keep handing out invitations until i reaches 10.

using System;

class Program
{
    static void Main()
    {
        // Start counting from 0
        int i = 0;
        
        // While loop to hand out invitations until i is 10
        while (i < 10)
        {
            Console.WriteLine("Handing out invitation #" + (i + 1) + " to guest!");
            i++; // Increase i to prevent an infinite loop
        }
    }
}

Explanation:

  • The while loop repeats the action of handing out invitations.
  • It continues until i reaches 10, and after each invitation, i increases by 1.
  • The output will be just like in the for loop example, but the loop condition is controlled with a while loop.

πŸ§‘β€πŸ’» Key Differences Between For Loop and While Loop πŸ§‘β€πŸ’»

  • For Loop: You know how many times you need to repeat something, so you set a specific range for the loop (e.g., 0 to 9).
  • While Loop: You might not know how many times the loop will run, but you keep checking a condition until it’s no longer true (e.g., repeat as long as i is less than 10).

πŸ“š Another Fun Example: Counting Down πŸ“š

Let’s use the while loop to count down from 10 to 1. This is perfect for a countdown timer, like before launching a rocket πŸš€.

using System;

class Program
{
    static void Main()
    {
        int countdown = 10;
        
        // While loop to countdown from 10 to 1
        while (countdown > 0)
        {
            Console.WriteLine("Countdown: " + countdown);
            countdown--; // Decrease the number to count down
        }
        
        Console.WriteLine("Blast off! πŸš€");
    }
}

Explanation:

  • We start the countdown at 10, and the while loop continues as long as countdown is greater than 0.
  • Each time, we decrease countdown by 1 until it reaches 0. When that happens, we print "Blast off!"

πŸ’‘ When to Use Which Loop? πŸ’‘

  • Use a for loop when you know exactly how many times you want to repeat something.

    • Example: Counting invitations from 1 to 10.
  • Use a while loop when you want to repeat something as long as a condition is true, and you may not know how many times it will take.

    • Example: Counting down from 10 to 1 until the countdown reaches 0.