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 asi
is less than 10.i++
: This increasesi
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 withi
set to 0.while (i < 10)
: The condition for the loop to keep running. As long asi
is less than 10, it will repeat.i++
: Just like the for loop, we increasei
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.