Control flow statements in C# are used to control the flow of execution in a program. These statements allow you to execute code based on certain conditions, loop through code multiple times, and perform other actions that can help you create more powerful and dynamic applications.
In this article, we’ll explore the different types of control flow statements in C# and discuss how to use them in your own code.
If Statements
The if statement is one of the most commonly used control flow statements in C#. It allows you to execute a block of code if a certain condition is met. For example:
int age = 25;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
In this example, the code inside the curly braces will only be executed if the age variable is greater than or equal to 18.
Else Statements
You can also use an else statement with an if statement to execute a different block of code if the condition is not met. For example:
int age = 15;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are not yet an adult.");
}
In this example, if the age variable is less than 18, the code inside the else block will be executed instead.
Switch Statements
Switch statements are another type of control flow statement that allow you to execute different blocks of code based on the value of a variable. For example:
int dayOfWeek = 3;
switch (dayOfWeek)
{
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;
default:
Console.WriteLine("Weekend");
break;
}
In this example, the code inside the case block for the value of the dayOfWeek variable will be executed. If the value does not match any of the cases, the code inside the default block will be executed.
Loops
Loops are another important type of control flow statement that allow you to execute code multiple times. There are three types of loops in C#: for, while, and do-while.
For Loops
For loops are used when you know how many times you want to loop through a block of code. For example:
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
In this example, the code inside the curly braces will be executed five times, with the value of i starting at 0 and incrementing by 1 each time through the loop.
While Loops
While loops are used when you want to loop through a block of code while a certain condition is true. For example:
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
In this example, the code inside the curly braces will be executed as long as the value of i is less than 5.
Do-While Loops
Do-while loops are similar to while loops, but the code inside the loop is executed at least once before the condition is checked. For example:
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);
In this example, the code inside the curly braces will be executed once before the condition is checked. If the value of i is less than 5, the code will be executed again.
Conclusion
Control flow statements are powerful tools in C# that allow you to control the flow of execution in your program. If statements are used to execute code based on certain conditions, while switch statements allow you to execute different blocks of code based on the value of a variable.
Loops are another important type of control flow statement that allow you to execute code multiple times. For loops are used when you know how many times you want to loop through a block of code, while while and do-while loops are used when you want to loop through a block of code while a certain condition is true.
By using these control flow statements in your C# programs, you can create more dynamic and powerful applications that can respond to user input and handle complex logic with ease
One thought on “Control Flow Statements in C#”