C# Control Structures Examples
C# Control Structures Examples
Control structures are a fundamental part of programming languages. They allow you to control the flow of execution of your code, depending on certain conditions or loop through a block of code multiple times. In C#, there are several types of control structures:
- Conditional statements: These are used to execute a certain block of code only if a certain condition is met. The two main types of conditional statements in C# are
if
andswitch
.
Example:
int x = 10;
if (x > 5)
{
Console.WriteLine("x is greater than 5");
}
- Loops: These allow you to execute a block of code multiple times, until a certain condition is met. The three main types of loops in C# are
for
,while
, anddo-while
.
Example:
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
- Jump statements: These allow you to transfer control to a different part of the code, based on certain conditions. The main jump statements in C# are
break
,continue
,goto
, andreturn
.
Example:
int x = 10;
while (x > 0)
{
if (x == 5)
{
break;
}
Console.WriteLine(x);
x--;
}
I hope this helps! Let me know if you have any questions.
Post a Comment
image video quote pre code