C# Control Flow Statements

Introduction

Control flow statements in C# are used to control the execution flow of the program based on certain conditions. These statements allow you to create logic that can make decisions, repeat operations, and handle different cases in a structured and readable way. Understanding control flow statements is essential for writing effective and efficient C# programs.

Types of Control Flow Statements

  1. Conditional Statements

    • if
    • else
    • else if
    • switch
  2. Looping Statements

    • for
    • foreach
    • while
    • do-while
  3. Jump Statements

    • break
    • continue
    • goto
    • return

Conditional Statements

if Statement

The if statement is used to execute a block of code only if a specified condition is true.

Syntax

if (condition)
{
    // Code to execute if the condition is true
}

Example

int number = 10;

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

else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

Syntax

if (condition)
{
    // Code to execute if the condition is true
}
else
{
    // Code to execute if the condition is false
}

Example

int number = 3;

if (number > 5)
{
    Console.WriteLine("The number is greater than 5.");
}
else
{
    Console.WriteLine("The number is not greater than 5.");
}

else if Statement

The else if statement is used to specify a new condition if the first condition is false.

Syntax

if (condition1)
{
    // Code to execute if condition1 is true
}
else if (condition2)
{
    // Code to execute if condition1 is false and condition2 is true
}
else
{
    // Code to execute if both condition1 and condition2 are false
}

Example

int number = 7;

if (number > 10)
{
    Console.WriteLine("The number is greater than 10.");
}
else if (number > 5)
{
    Console.WriteLine("The number is greater than 5 but less than or equal to 10.");
}
else
{
    Console.WriteLine("The number is 5 or less.");
}

switch Statement

The switch statement is used to select one of many code blocks to be executed based on the value of a variable or expression.

Syntax

switch (variable)
{
    case value1:
        // Code to execute if variable == value1
        break;
    case value2:
        // Code to execute if variable == value2
        break;
    // Add more cases as needed
    default:
        // Code to execute if variable does not match any case
        break;
}

Example

int day = 3;

switch (day)
{
    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;
    case 6:
        Console.WriteLine("Saturday");
        break;
    case 7:
        Console.WriteLine("Sunday");
        break;
    default:
        Console.WriteLine("Invalid day");
        break;
}

Looping Statements

for Loop

The for loop is used to execute a block of code a specified number of times.

Syntax

for (initialization; condition; increment)
{
    // Code to execute
}

Example

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

foreach Loop

The foreach loop is used to iterate over a collection (e.g., an array or a list).

Syntax

foreach (dataType item in collection)
{
    // Code to execute for each item
}

Example

int[] numbers = { 1, 2, 3, 4, 5 };

foreach (int number in numbers)
{
    Console.WriteLine("Number: " + number);
}

while Loop

The while loop is used to execute a block of code as long as a specified condition is true.

Syntax

while (condition)
{
    // Code to execute
}

Example

int i = 0;

while (i < 5)
{
    Console.WriteLine("i = " + i);
    i++;
}

do-while Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once.

Syntax

do
{
    // Code to execute
} while (condition);

Example

int i = 0;

do
{
    Console.WriteLine("i = " + i);
    i++;
} while (i < 5);

Jump Statements

break Statement

The break statement is used to exit a loop or a switch statement immediately.

Example

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

continue Statement

The continue statement is used to skip the rest of the code in the current iteration of a loop and move to the next iteration.

Example

for (int i = 0; i < 10; i++)
{
    if (i % 2 == 0)
    {
        continue;
    }
    Console.WriteLine("i = " + i);
}

goto Statement

The goto statement is used to transfer control to a labeled statement. Its use is generally discouraged as it can make the code difficult to follow.

Syntax

goto label;
...
label:
// Code to execute

Example

int x = 10;

start:
if (x > 0)
{
    Console.WriteLine("x = " + x);
    x--;
    goto start;
}

return Statement

The return statement is used to exit a method and optionally return a value.

Example

int Add(int a, int b)
{
    return a + b;
}

Conclusion

Control flow statements in C# are essential for creating dynamic and flexible programs. They allow you to make decisions, repeat operations, and handle different scenarios in a structured manner. By understanding and effectively using these statements, you can write more robust and efficient code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top