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