Control Flow
## Learning Objectives
- Master if, else if, else statements
- Understand switch statements
- Learn for, while, do-while loops
- Use break and continue
- Master pattern matching
## if Statement
### Basic if
```csharp
int age = 18;
if (age >= 18)
{
Console.WriteLine("Adult");
}
```
### if-else
```csharp
int age = 15;
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
```
### if-else if-else
```csharp
int score = 85;
char grade;
if (score >= 90)
{
grade = 'A';
}
else if (score >= 80)
{
grade = 'B';
}
else if (score >= 70)
{
grade = 'C';
}
else if (score >= 60)
{
grade = 'D';
}
else
{
grade = 'F';
}
Console.WriteLine("Grade: " + grade);
```
## Ternary Operator
```csharp
int age = 20;
string status = (age >= 18) ? "adult" : "minor";
```
## switch Statement
### Basic switch
```csharp
int day = 3;
string dayName;
switch (day)
{
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid";
break;
}
Console.WriteLine(dayName);
```
### Switch Expression (C# 8+)
```csharp
string dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid"
};
```
### Multiple Cases
```csharp
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28;
break;
}
```
### Switch with when Clause
```csharp
switch (value)
{
case int i when i > 0:
Console.WriteLine("Positive integer");
break;
case int i when i < 0:
Console.WriteLine("Negative integer");
break;
case null:
Console.WriteLine("Null");
break;
}
```
## Pattern Matching
### is Pattern
```csharp
object obj = "Hello";
if (obj is string s)
{
Console.WriteLine($"String length: {s.Length}");
}
```
### switch Pattern Matching
```csharp
string Describe(object obj) => obj switch
{
int i when i > 0 => $"Positive integer {i}",
int i when i < 0 => $"Negative integer {i}",
int i => $"Zero {i}",
string s => $"String of length {s.Length}",
null => "Null",
_ => "Something else"
};
```
## for Loop
### Basic for
```csharp
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i); // 0, 1, 2, 3, 4
}
```
### For-Each
```csharp
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
Console.WriteLine(num);
}
```
### With Multiple Variables
```csharp
for (int i = 0, j = 10; i < 5; i++, j--)
{
Console.WriteLine($"{i} - {j}");
}
```
## while Loop
### Basic while
```csharp
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
```
### while with Condition First
```csharp
string input;
while (!string.IsNullOrEmpty(input = Console.ReadLine()))
{
Console.WriteLine("You entered: " + input);
}
```
## do-while Loop
### Executes at Least Once
```csharp
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);
```
### Menu Example
```csharp
int choice;
do
{
Console.WriteLine("1. New Game");
Console.WriteLine("2. Load Game");
Console.WriteLine("3. Quit");
Console.Write("Choose: ");
choice = int.Parse(Console.ReadLine());
} while (choice < 1 || choice > 3);
```
## break and continue
### break - Exit Loop
```csharp
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // Exit when i is 5
}
Console.WriteLine(i); // 0, 1, 2, 3, 4
}
```
### continue - Skip Iteration
```csharp
for (int i = 0; i < 5; i++)
{
if (i == 2)
{
continue; // Skip when i is 2
}
Console.WriteLine(i); // 0, 1, 3, 4
}
```
## Nested Loops
```csharp
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"({i},{j}) ");
}
Console.WriteLine();
}
// Output:
// (0,0) (0,1) (0,2)
// (1,0) (1,1) (1,2)
// (2,0) (2,1) (2,2)
```
## goto
### Labeled Statements
```csharp
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (j == 2)
{
goto End;
}
Console.WriteLine($"{i},{j}");
}
}
End:
Console.WriteLine("Done");
```
## Common Patterns
### Sum 1 to N
```csharp
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum += i;
}
```
### Find in Array
```csharp
int[] numbers = { 3, 7, 2, 9, 4 };
int target = 9;
foreach (int num in numbers)
{
if (num == target)
{
Console.WriteLine("Found!");
break;
}
}
```
### Count Matches
```csharp
int[] numbers = { 1, 2, 3, 2, 4, 2, 5 };
int target = 2;
int count = 0;
foreach (int num in numbers)
{
if (num == target)
{
count++;
}
}
Console.WriteLine("Count: " + count);
```
## Summary
- if/else if/else for conditional logic
- switch for multiple discrete values
- switch expression for modern C# 8+
- Pattern matching with is and switch
- for loop for known iterations
- foreach for collection iteration
- while loop for condition-based iteration
- do-while for at-least-once iteration
- break exits loop; continue skips iteration
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →