5️⃣ C# Control Flow & Decision Making: Loops, Conditions, Break & Continue Explained
Control flow allows developers to define how and when blocks of code are executed. It includes decisions, loops, and flow-altering commands such as break and continue.
Introduction – Why Learn C# Control Flow?
Control flow structures are essential in any C# application to execute logic conditionally or repeatedly. These constructs form the logic gates of your software, allowing your programs to make decisions and iterate over data.
In this guide, you’ll learn:
- How to control program execution using decisions and loops
- The usage of
if,switch, and all types of loops - How to manage loop control with
breakandcontinue
Topics Covered
| Subtopic | Description |
|---|---|
| C# If / If…Else Statement | Conditional logic using if conditions |
| C# Nested If | Multilevel decision-making |
| C# Switch / Nested Switch | Multi-condition branching using case matching |
| C# For Loop | Pre-defined number of iterations |
| C# While Loop | Conditional repetition until the condition is false |
| C# Do While Loop | Loop executes at least once before condition check |
| C# Nested Loops | Loop inside another loop for multidimensional iteration |
| C# Break and Continue | Exit or skip loop iterations based on conditions |
C# If Statement / If…Else Statement
int score = 85;
if (score >= 90)
{
Console.WriteLine("Excellent");
}
else
{
Console.WriteLine("Keep Trying");
}
Executes the appropriate block depending on the condition’s truth value.
C# Nested If
int marks = 75;
if (marks >= 60)
{
if (marks >= 90)
Console.WriteLine("Grade: A+");
else
Console.WriteLine("Grade: B");
}
else
{
Console.WriteLine("Grade: C");
}
Allows deeper decision trees inside condition blocks.
C# Switch Statement / Nested Switch
int choice = 2;
switch (choice)
{
case 1:
Console.WriteLine("Option 1");
break;
case 2:
Console.WriteLine("Option 2");
break;
default:
Console.WriteLine("Invalid Option");
break;
}
Matches and executes based on discrete values.
C# For Loop
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Step " + i);
}
Best for known iteration counts.
C# While Loop
int i = 1;
while (i <= 5)
{
Console.WriteLine("Count: " + i);
i++;
}
Repeats as long as the condition is true.
C# Do While Loop
int x = 1;
do
{
Console.WriteLine("Execute once");
x++;
} while (x < 1);
Executes block at least once, regardless of condition.
C# Nested Loops
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 2; j++)
{
Console.WriteLine($"i = {i}, j = {j}");
}
}
Ideal for grid/matrix operations or pattern generation.
C# Break and Continue
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue; // Skip iteration
if (i == 4)
break; // Exit loop
Console.WriteLine(i);
}
break exits the loop; continue skips to next iteration.
Summary – Recap & Next Steps
Mastering control flow in C# equips you to handle complex logic structures efficiently, whether you’re validating input, building menus, or processing data.
Key Takeaways:
- Use
if,switch, and loops to guide execution - Nested structures handle complex conditions
breakandcontinuegive precise control over loops
Real-World Relevance: Foundational for all applications, from games to enterprise software.
FAQs
Q: When should I use switch over if?
Use switch when checking a variable against multiple constant values.
Q: Does do…while always execute the loop body?
Yes, do…while runs the loop body once before checking the condition.
Q: Can loops be nested indefinitely?
Technically yes, but deeply nested loops are discouraged due to readability and performance issues.
Q: What’s the difference between continue and break?
continue skips the current loop iteration; break exits the loop entirely.
Share Now :
