C# do While loop Tutorial – A Fun and Easy Guide!

Ever Tried Unlocking Your Phone? πŸ“±

Imagine you’re trying to unlock your phone. You must enter your PIN or use your fingerprint at least once to check if it’s correct. If it’s wrong, your phone keeps asking for the right PIN until you enter it correctly (or get locked out! πŸ˜…).

That’s exactly how a do-while loop works in C#! It runs at least once, no matter what, and then keeps running as long as a condition is met. πŸš€

Understanding the do-while Loop

Syntax of do-while Loop in C#

				
					do
{
    // Code to execute at least once
} while (condition);
				
			

πŸ”₯ Key takeaway? The loop executes the code first and checks the condition later.

Basic do While loop Example C#

Let’s start with something simple. Say we want to print numbers from 1 to 5.

				
					using System;

class Program
{
    static void Main()
    {
        int num = 1; 

        do
        {
            Console.WriteLine("Number: " + num);
            num++;
        } while (num <= 5);
    }
}
				
			

Output

				
					Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
				
			

Explanation

1️⃣ The do block runs first and prints “Number: 1”.
2️⃣ Then, it checks the condition (num <= 5). Since it’s true, it loops again.
3️⃣ This repeats until num becomes 6, at which point the condition fails, and the loop stops.

do-while vs while Loop: What's the Difference? πŸ€”

The major difference is:

βœ… do-while runs at least once (even if the condition is false from the start).
βœ… while first checks the condition, and if it’s false, it never runs.

Check this out:

				
					int count = 10;

while (count < 5)  // Condition is false from the start
{
    Console.WriteLine("This will never run!");
}

do
{
    Console.WriteLine("This will run at least once!");
} while (count < 5);
				
			

Output

				
					This will run at least once!
				
			

πŸ‘‰ The while loop doesn’t run at all.
πŸ‘‰ The do-while loop runs once before realizing the condition is false.

Real-World Example: User Login Simulation πŸ”‘

Imagine a login system where a user gets one chance to enter their password and keeps trying if they fail.

				
					using System;

class Program
{
    static void Main()
    {
        string correctPassword = "hello123";
        string userInput;

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

        Console.WriteLine("Access Granted! πŸŽ‰");
    }
}
				
			

Output (Example Run)

				
					Enter your password: test
Enter your password: 1234
Enter your password: hello123
Access Granted! πŸŽ‰
				
			

How it Works?

βœ… The user must enter their password at least once.
βœ… If it’s incorrect, the loop repeats.
βœ… Once the correct password is entered, it stops and prints “Access Granted!”.

Conclusion 🎯

So, you just mastered the do-while loop in C#! πŸš€ It’s perfect when you need to execute code at least once before checking a condition. Whether it’s game retries, login attempts, or menu loops, this loop has got your back! πŸ’ͺ

πŸ’‘ Remember: If you want a loop to always run at least once, the do-while loop is your best friend!

πŸ‘‰ If you have difficulty or a question, drop a comment. We will be happy to help you! 😊

Next What? πŸš€

Guess what? In the next chapter, you’ll learn about Parallel.For loop in C#! πŸ”₯ Ever wondered how to run multiple loops at the same time to boost performance? Well, you’re in for a treat! Stay tuned! πŸ˜ƒ

Leave a Comment

Share this Doc

Do while loop

Or copy link