Appearance
Control Flow
Control flow statements determine the order in which code is executed in a program. They allow you to make decisions, repeat actions, and control the flow of your application based on different conditions.
If Statements
If statements allow you to execute different code blocks based on conditions. They are fundamental for decision-making in your programs.
csharp
if (condition)
{
// code executed when condition is true
}
else if (anotherCondition)
{
// code executed when anotherCondition is true
}
else
{
// code executed when no conditions are true
}
Switch Statement
Switch statements provide a clean way to execute different code blocks based on a single value. They're especially useful when dealing with multiple possible values.
csharp
switch (value)
{
case 1:
// code for value 1
break;
case 2:
// code for value 2
break;
default:
// code when no cases match
break;
}
Loops
Loops allow you to execute code repeatedly based on certain conditions.
For Loop
For loops are ideal when you know exactly how many times you want to repeat an action.
csharp
for (int i = 0; i < 10; i++)
{
// code executed 10 times
}
While Loop
While loops continue executing as long as a condition remains true.
csharp
while (condition)
{
// code executed while condition is true
}
Foreach Loop
Foreach loops are designed to iterate over collections of items.
csharp
foreach (var item in collection)
{
// code executed for each item
}