← Back to Articles         
C# Control Flow Statements - Part 1

C# Control Flow Statements - Part 1

Published on 12 Oct 2026     4 min read C#
Control Statements & Loops
 

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

if-else

switch

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-else for complex conditions.

  • Use switch when 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

for

foreach

while

do-while

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

  • for is best when the number of iterations is known.

  • foreach is best for reading collection elements.

  • while is useful when the iteration count is unknown.

  • do-while always executes at least once.

  • foreach does 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

break

continue

return

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 break with return, the entire Test() 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

  • break terminates the nearest enclosing loop or switch.

  • continue skips the remaining statements in the current loop iteration.

  • return exits the current method immediately.

  • return can also return a value to the calling method.

  • In nested loops, break exits only the innermost loop.

 

Quick Interview Revision

Question

Remember

if-else vs switch

Conditions vs multiple cases

Ternary operator

Short conditional expression

for vs foreach

Counter vs collection

while vs do-while

Check first vs execute first

break

Exit loop

continue

Skip iteration

return

Exit method

Interview Tip: The most commonly tested differences are while vs do-while, for vs foreach, and break vs continue vs return.

← Back to Articles