Control Flow in C#
Control flow statements allow you to control the execution of your code based on certain conditions or to repeat a block of code multiple times. In this lesson, we'll cover the most common control flow statements and loops in C#.
If Statements
The if
statement is used to execute a block of code only if a specified condition is true.
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
If-Else Statements
The if-else
statement is used to execute one block of code if a condition is true and another block of code if it is false.
int age = 16;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are a minor.");
}
You can also use the else if
statement to check multiple conditions:
int age = 20;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else if (age >= 13)
{
Console.WriteLine("You are a teenager.");
}
else
{
Console.WriteLine("You are a minor.");
}
Switch Statements
The switch
statement is used to execute different blocks of code based on the value of a variable.
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid day.");
break;
}
Switch Expressions
Switch expressions provide a more concise way to write switch statements.
int day = 3;
string dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid day"
};
Console.WriteLine(dayName);
Loops
For Loop
The for
loop is used to execute a block of code repeatedly for a specific number of times.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
The first part of the for
is for variable declarations, the second part is the "check" to see if the loop should be executed, and the last statement is typically used to increment the variable declared in the first statement.
Fun fact: each of the statements inside of the for
are weirdly flexible, like making multiple variable declarations or straight up excluding the statement altogether. You should mess with them sometime.
🌶️🌶️🌶️ This is the most ubiquitious type of for
loop, but I rarely write them myself anymore!
While Loop
The while
loop is used to execute a block of code repeatedly until a specified condition is false.
while (condition)
{
// code block to be executed
}
I mostly see while(true)
loops in codebases that do something over and over again, but then break out of the loop with a break
statement:
while (true)
{
// code block to be executed
if (condition)
{
break;
}
}
Useful, but can be a little dangerous as infinite loops are easy to create!
Do-While Loop
The do-while
loop is similar to the while
loop, but it ensures the block of code is executed at least once before checking the condition.
do
{
// code block to be executed
}
while (condition);
🌶️🌶️🌶️ I don't often see these in the wild. I write them very sparingly - maybe once a year if that.
Foreach Loop
The foreach
loop is used to iterate over each element in a collection.
foreach (var item in collection)
{
// code block to be executed
}
Probably my favorite and most used loop type. More when we get to collections!
Break and Continue
Break
The break
statement is used to exit a loop prematurely.
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
Continue
The continue
statement is used to skip the rest of the code in a loop for the current iteration and continue with the next iteration.
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
continue;
}
}