A while loop helps you repeat a set of actions as long as a condition is true. It saves time by avoiding writing the same code again and again.
While loop execution model in C Sharp (C#)
Introduction
Syntax
C Sharp (C#)
while (condition) { // code to repeat }
The condition is checked before each repetition.
If the condition is false at the start, the code inside the loop does not run at all.
Examples
C Sharp (C#)
int count = 0; while (count < 3) { Console.WriteLine(count); count++; }
keepGoing becomes false inside the loop.C Sharp (C#)
bool keepGoing = true; while (keepGoing) { Console.WriteLine("Running..."); keepGoing = false; }
Sample Program
This program prints numbers from 1 to 5 using a while loop. It shows how the loop runs repeatedly while the condition is true.
C Sharp (C#)
using System; class Program { static void Main() { int number = 1; while (number <= 5) { Console.WriteLine($"Number is {number}"); number++; } } }
Important Notes
Make sure the condition will eventually become false, or the loop will run forever (infinite loop).
You can use break; inside the loop to stop it early if needed.
Summary
A while loop repeats code as long as a condition is true.
The condition is checked before each repetition.
Use while loops when you don't know how many times you need to repeat something.
Practice
1. What does a
while loop do in C#?easy
Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly while its condition remains true.Step 2: Compare with other options
Options B, C, and D do not describe the behavior of a while loop correctly.Final Answer:
Repeats a block of code as long as a condition is true -> Option DQuick Check:
while loop = repeats while condition true [OK]
Hint: While loops run only if condition is true before each repeat [OK]
Common Mistakes:
- Thinking while loops run a fixed number of times
- Confusing while with do-while loops
- Assuming code runs at least once without checking condition
2. Which of the following is the correct syntax for a
while loop in C#?easy
Solution
Step 1: Recall correct while loop syntax
The correct syntax requires parentheses around the condition and braces for the code block.Step 2: Identify syntax errors in other options
A has a colon after while, B misses parentheses around the condition, D has a semicolon after the condition making the loop body empty.Final Answer:
while (condition) { /* code to repeat */ } -> Option AQuick Check:
Correct syntax = while (condition) { /* code to repeat */ } [OK]
Hint: Always use parentheses for condition and braces for loop body [OK]
Common Mistakes:
- Omitting parentheses around condition
- Adding semicolon after while condition
- Using colon instead of parentheses
3. What will be the output of this C# code?
int count = 3;
while (count > 0) {
Console.Write(count + " ");
count--;
}medium
Solution
Step 1: Trace the loop iterations
count starts at 3, prints 3, then decrements to 2, prints 2, then 1, prints 1, then decrements to 0 and stops.Step 2: Confirm output sequence
The printed numbers are "3 2 1 " exactly in that order.Final Answer:
3 2 1 -> Option BQuick Check:
Loop prints count down from 3 to 1 [OK]
Hint: Count down loops print from start to 1 before stopping [OK]
Common Mistakes:
- Including 0 in output
- Printing numbers in ascending order
- Forgetting to decrement count
4. Identify the error in this code snippet:
int i = 0;
while (i < 5)
Console.Write(i);
i++;medium
Solution
Step 1: Analyze loop body without braces
Only the first statement after while loops; here, only Console.Write(i) is inside the loop.Step 2: Understand effect on i++
i++ runs once after the loop finishes, so i never increments inside the loop, causing an infinite loop if braces were not considered.Final Answer:
Missing braces causes only Console.Write(i) to loop, i++ runs once -> Option AQuick Check:
Braces needed to include i++ in loop [OK]
Hint: Use braces to include multiple statements in while loop [OK]
Common Mistakes:
- Assuming both statements are inside loop without braces
- Thinking missing semicolon after while is error
- Confusing infinite loop cause
5. You want to read user input repeatedly until they type "exit". Which while loop correctly implements this in C#?
hard
Solution
Step 1: Understand the need to read input before loop
We must read input once before the while condition to avoid skipping the first input.Step 2: Check each option's logic
string input = Console.ReadLine(); while (input != "exit") { input = Console.ReadLine(); } uses priming read pattern correctly. string input; do { input = Console.ReadLine(); } while (input != "exit"); is do-while, not while loop. while (true) { string input = Console.ReadLine(); if (input != "exit") break; } breaks incorrectly on non-exit input, stopping too early. string input; while (input != "exit") { input = Console.ReadLine(); } uses uninitialized input, compile error.Final Answer:
string input = Console.ReadLine(); while (input != "exit") { input = Console.ReadLine(); } -> Option CQuick Check:
Read input before while, then loop while not "exit" [OK]
Hint: Read input once before while to check condition correctly [OK]
Common Mistakes:
- Not reading input before while loop
- Using do-while instead of while
- Using infinite loop with break instead of while condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
