C# Control Flow Statements – Interview Questions
1. What is the difference between if-else, switch, and the ternary operator?
All three are used for decision-making in C#, but they are suitable for different situations.
Differences
|
Feature |
|
|
Ternary ( |
|---|---|---|---|
|
Purpose |
Check conditions |
Select matching case |
Choose between two values |
|
Conditions |
Simple or complex |
Multiple cases/patterns |
One Boolean condition |
|
Code length |
Medium |
Medium |
Short |
|
Best for |
Ranges, multiple conditions |
Multiple fixed choices |
Simple assignments |
Simple Example
int marks = 75;
// if-else
if (marks >= 50)
Console.WriteLine("Pass");
else
Console.WriteLine("Fail");
// switch
switch (marks)
{
case >= 50:
Console.WriteLine("Pass");
break;
default:
Console.WriteLine("Fail");
break;
}
// Ternary
string result = marks >= 50 ? "Pass" : "Fail";
Console.WriteLine(result);
Output (all three): Pass
Explanation
-
if-else: Checks whether marks are greater than or equal to 50.
-
switch: Matches the marks against a relational pattern.
-
Ternary: Returns
"Pass"or"Fail"based on the condition.
Key Points
-
Use
if-elsefor complex conditions. -
Use
switchwhen selecting among multiple cases. -
Use the ternary operator for simple conditional assignments.
-
Avoid nested ternary operators when they reduce readability.
2. What is the difference between for, foreach, while, and do-while?
All four are looping statements used to execute code repeatedly.
Differences
|
Feature |
|
|
|
|
|---|---|---|---|---|
|
Purpose |
Counter-based loop |
Iterate collection |
Condition-based loop |
Execute before checking |
|
Condition check |
Before |
Per iteration |
Before |
After |
|
Minimum executions |
0 |
0 |
0 |
1 |
|
Best for |
Known count |
Arrays/lists |
Unknown count |
Execute at least once |
Simple Example
// for
for (int i = 1; i <= 3; i++)
Console.WriteLine(i);
// foreach
int[] numbers = { 1, 2, 3 };
foreach (int number in numbers)
Console.WriteLine(number);
// while
int x = 1;
while (x <= 3)
Console.WriteLine(x++);
// do-while
int y = 1;
do
{
Console.WriteLine(y++);
}
while (y <= 3);
Output (each loop):
1
2
3
Explanation
-
for: Runs three times using a counter.
-
foreach: Reads each element from the array.
-
while: Checks the condition before executing.
-
do-while: Executes first, then checks the condition.
Execution Flow
for / while
|
Check Condition
|
+-- True → Execute → Repeat
|
+-- False → Exit
do-while
|
Execute
|
Check Condition
|
+-- True → Repeat
|
+-- False → Exit
Key Points
-
foris best when the number of iterations is known. -
foreachis best for reading collection elements. -
whileis useful when the iteration count is unknown. -
do-whilealways executes at least once. -
foreachdoes not require manual index management.
3. What is the difference between break, continue, and return?
These statements control the execution flow of loops and methods.
Differences
|
Feature |
|
|
|
|---|---|---|---|
|
Purpose |
Exit loop/switch |
Skip current iteration |
Exit method |
|
Stops loop? |
Yes |
No |
Yes, if inside method's loop |
|
Stops method? |
No |
No |
Yes |
|
Common usage |
Stop searching |
Skip unwanted items |
Return result |
Simple Example
static void Test()
{
for (int i = 1; i <= 5; i++)
{
if (i == 2)
continue;
if (i == 4)
break;
Console.WriteLine(i);
}
Console.WriteLine("Completed");
}
Output:
1
3
Completed
Explanation
-
continue: When
i == 2, skips printing 2 and proceeds to the next iteration. -
break: When
i == 4, immediately exits the loop. -
return: If we replace
breakwithreturn, the entireTest()method exits, so"Completed"will not print.
Execution Flow
Loop
|
+-- continue → Skip current iteration
| |
| +→ Next iteration
|
+-- break → Exit loop
| |
| +→ Continue after loop
|
+-- return → Exit entire method
Key Points
-
breakterminates the nearest enclosing loop or switch. -
continueskips the remaining statements in the current loop iteration. -
returnexits the current method immediately. -
returncan also return a value to the calling method. -
In nested loops,
breakexits only the innermost loop.
Quick Interview Revision
|
Question |
Remember |
|---|---|
|
|
Conditions vs multiple cases |
|
Ternary operator |
Short conditional expression |
|
|
Counter vs collection |
|
|
Check first vs execute first |
|
|
Exit loop |
|
|
Skip iteration |
|
|
Exit method |
Interview Tip: The most commonly tested differences are while vs do-while, for vs foreach, and break vs continue vs return.