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:

  1. 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 and switch.

Example:

int x = 10;

if (x > 5)
{
    Console.WriteLine("x is greater than 5");
}


  1. 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, and do-while.

Example:

for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}

  1. 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, and return.

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.