Complete C# Tutorial

C# While Loop Tutorial โ€“ Simple While Loop Example C#

Imagine Thisโ€ฆ

You’re playing a game ๐ŸŽฎ, and you keep collecting coins until you reach 100. You donโ€™t know exactly how many rounds it will take, but you keep going as long as your total coins are less than 100.

Thatโ€™s exactly how a while loop works! It keeps running until a certain condition is met. Sounds cool, right? ๐Ÿ˜ƒ

Let’s explore how it works with real-world examples!

What is a While Loop in C#?

A while loop is used when you donโ€™t know exactly how many times you need to repeat something. It keeps running as long as the condition is true.

๐Ÿ’ก Think of it like this: You keep eating ๐Ÿ• while youโ€™re hungry. The moment youโ€™re full, you stop.

Syntax of While Loop in C#

				
					while (condition)
{
    // Code to execute while condition is true
}
				
			

๐Ÿ‘‰ The loop keeps running as long as the condition is true.
๐Ÿ‘‰ The moment the condition turns false, the loop stops.

While Loop Example C# โ€“ Collecting Coins ๐Ÿ’ฐ

Letโ€™s say youโ€™re playing a game. You need to collect at least 100 coins to win. Letโ€™s see how a while loop helps you!

Example 1: Using While Loop to Collect Coins

				
					using System;

class Program
{
    static void Main()
    {
        int coins = 0; // Starting with 0 coins

        Console.WriteLine("Collecting coins...");

        // Keep collecting coins while we have less than 100
        while (coins < 100)
        {
            coins += 10; // Collect 10 coins in each round
            Console.WriteLine($"Collected 10 coins! Total now: {coins}");
        }

        Console.WriteLine("You have 100 coins! ๐ŸŽ‰ You win!");
    }
}
				
			

Expected Output

				
					Collecting coins...
Collected 10 coins! Total now: 10
Collected 10 coins! Total now: 20
Collected 10 coins! Total now: 30
...
Collected 10 coins! Total now: 100
You have 100 coins! ๐ŸŽ‰ You win!
				
			

How Does This Work?

  1. Step 1: We start with 0 coins.
  2. Step 2: The while loop keeps running while coins are less than 100.
  3. Step 3: Each time, we add 10 coins.
  4. Step 4: When we hit 100, the loop stops, and we win the game! ๐ŸŽ‰

Example 2: Countdown Timer โณ

Let’s say you’re waiting for a rocket launch ๐Ÿš€, and we need a countdown. A while loop is perfect for this!

				
					using System;

class Program
{
    static void Main()
    {
        int countdown = 5; // Start countdown from 5

        Console.WriteLine("Rocket launch in:");

        while (countdown > 0)
        {
            Console.WriteLine(countdown);
            countdown--; // Reduce countdown by 1
        }

        Console.WriteLine("๐Ÿš€ Blast Off!");
    }
}
				
			

Expected Output

				
					Rocket launch in:
5
4
3
2
1
๐Ÿš€ Blast Off!
				
			

How Does This Work?

  1. Step 1: Start from 5.
  2. Step 2: The while loop runs until countdown hits 0.
  3. Step 3: Each time, we decrease the number by 1.
  4. Step 4: When it reaches 0, we print "Blast Off!". ๐Ÿš€

Example 3: Asking for Correct Password ๐Ÿ”’

Imagine a login system where the user must enter the correct password to continue. We use a while loop to keep asking until they enter the right one!

				
					using System;

class Program
{
    static void Main()
    {
        string password = "secret";  // Correct password
        string userInput = "";       // User input

        while (userInput != password)
        {
            Console.Write("Enter password: ");
            userInput = Console.ReadLine();
        }

        Console.WriteLine("โœ… Access granted!");
    }
}
				
			

Expected Output (if user enters incorrect password first)

				
					Enter password: 1234
Enter password: hello
Enter password: secret
โœ… Access granted!
				
			

How Does This Work?

  1. Step 1: The correct password is "secret".
  2. Step 2: The loop keeps running until the user enters the right password.
  3. Step 3: Once correct, it stops and grants access.

Why Should You Use a While Loop?

๐ŸŽฏ Perfect for unknown repetitions โ€“ Runs until the condition is met.
๐Ÿš€ Ideal for input validation โ€“ Keep asking for correct data.
๐Ÿ“ Great for real-world tasks โ€“ Timers, login systems, collecting data.

Final Thoughts ๐Ÿค”

The while loop is super useful when you donโ€™t know how many times youโ€™ll need to repeat something. Whether you’re collecting coins, waiting for a launch, or asking for a password, while loops have got your back!

If you have any questions or got stuck, drop a comment! Weโ€™d love to help! ๐Ÿ˜Š

Next What? ๐Ÿš€

In the next chapter, youโ€™ll learn about do-while loops in C#! Itโ€™s like a while loop, but it always runs at least once. Stay tuned, and letโ€™s keep coding together! ๐Ÿ’ก

Leave a Comment

Share this Doc

While loop

Or copy link