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. π
What You Are Going to Learn in This Lesson
βοΈ What is a do-while loop?
βοΈ How is it different from a while loop?
βοΈ A simple do While loop Example C#
βοΈ Using it in real-world scenarios
βοΈ Complete code + output + explanation
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! π