MSDN Documentation

Microsoft Developer Network

C# Control Flow Statements

Control flow statements allow you to execute certain blocks of code based on conditions or to repeat execution. They are fundamental to writing dynamic and responsive applications.

Conditional Statements

Conditional statements execute code based on whether a specified condition evaluates to true or false.

if Statement

The if statement executes a block of code if a specified condition is true.

Example:

if (age >= 18)
{
    // Code to execute if age is 18 or greater
    "You are an adult.";
}

if-else Statement

The if-else statement executes one block of code if the condition is true and another block if the condition is false.

Example:

if (temperature < 0)
{
    "It's freezing!";
}
else
{
    "It's not freezing.";
}

if-else if-else Statement

This structure allows for multiple conditions to be checked in sequence.

Example:

if (score >= 90)
{
    "Grade: A";
}
else if (score >= 80)
{
    "Grade: B";
}
else
{
    "Grade: C or lower";
}

switch Statement

The switch statement selects one of many code blocks to be executed. It's often used as an alternative to a long if-else if-else chain when checking a variable against multiple constant values.

Example:

switch (dayOfWeek)
{
    case "Monday":
        "Start of the week.";
        break;
    case "Friday":
        "Almost the weekend!";
        break;
    default:
        "Mid-week.";
        break;
}

Iteration Statements (Loops)

Iteration statements allow you to execute a block of code repeatedly.

for Loop

The for loop is used when you know in advance how many times you want to execute a statement or a set of statements.

Example:

for (int i = 0; i < 5; i++)
{
    "Iteration: " + i;
}

while Loop

The while loop executes a statement or a set of statements as long as a specified Boolean expression evaluates to true.

Example:

while (count < 10)
{
    "Current count: " + count;
    count++;
}

do-while Loop

The do-while loop is similar to the while loop, but it executes the loop body at least once before checking the condition.

Example:

do
{
    "This will run at least once.";
    attempts--;
}
while (attempts > 0);

foreach Loop

The foreach loop iterates over a collection (like an array or list) to execute a statement or a set of statements for each element in the collection.

Example:

string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
    "Fruit: " + fruit;
}

Jump Statements

Jump statements transfer control from one part of the program to another.

break Statement

The break statement terminates the innermost enclosing switch, for, foreach, while, or do-while statement.

Example:

for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        break; // Exits the loop when i is 5
    }
    "Processing: " + i;
}

continue Statement

The continue statement skips the rest of the current iteration of the innermost enclosing loop (for, foreach, while, or do-while) and continues with the next iteration.

Example:

for (int i = 0; i < 5; i++)
{
    if (i % 2 == 0) // If i is even
    {
        continue; // Skip the rest of this iteration
    }
    "Odd number: " + i;
}

return Statement

The return statement exits the current method and optionally returns a value to the caller.

Example:

public int Add(int a, int b)
{
    return a + b; // Returns the sum of a and b
}

goto Statement

The goto statement is used to transfer control directly to another statement within the same method. Its use is generally discouraged due to potential for creating confusing code flow.

Example:

for (int i = 0; i < 10; i++)
{
    if (i == 3)
    {
        goto foundIt;
    }
}

foundIt:
    "Jumped to foundIt label.";