C#/C# Tutorial

C# Selection Statement ( If, Else, Else If, Switch ) | C# Tutorial for Beginners

DragonTory 2023. 2. 21. 22:02
반응형

C# Selection Statement ( If, Else, Else If, Switch )

In C#, selection statements allow you to make decisions based on a certain condition or value. 

The two main selection statements in C# are the if statement and the switch statement.

If statement


The if statement is used to execute a block of code if a certain condition is true, and can optionally include an else block to execute a different block of code if the condition is false. 

1. if : The if statement is used to execute a block of code if a certain condition is true.

The syntax is:

if (condition)
{
    // code to execute if condition is true
}

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

The syntax is:

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

 3. else if : The else if statement is used to check for multiple conditions in a single if statement.

The syntax is:

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

 

Here is an example:

int x = 5;
if (x > 10)
{
    Console.WriteLine("x is greater than 10");
}
else if (x > 5)
{
    Console.WriteLine("x is greater than 5, but not greater than 10");
}
else
{
    Console.WriteLine("x is not greater than 5");
}

In this example, 

the if statement first checks if x is greater than 10. 

If it is, it executes the first block of code. 

If it is not, it moves on to the next else if statement to check if x is greater than 5. 

If it is, it executes the second block of code. 

If it is not, it executes the else block.

 

Switch statement


The switch statement is used to execute different blocks of code depending on the value of a variable. 

The syntax is:

switch (variable)
{
    case value1:
        // code to execute if variable is equal to value1
        break;
    case value2:
        // code to execute if variable is equal to value2
        break;
    default:
        // code to execute if variable is not equal to any of the cases
        break;
}

 

Here is an example:

int dayOfWeek = 1;
switch (dayOfWeek)
{
    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;
    default:
        Console.WriteLine("Weekend");
        break;
}

In this example, the switch statement checks the value of dayOfWeek and executes the corresponding block of code. 

If dayOfWeek is equal to 1, it executes the first block of code and prints "Monday" to the console. 

If it is equal to 2, it executes the second block of code and prints "Tuesday" to the console. 

If it is equal to 3, it executes the third block of code and prints "Wednesday" to the console. 

If it is equal to 4, it executes the fourth block of code and prints "Thursday" to the console. 

If it is equal to 5, it executes the fifth block of code and prints "Friday" to the console. 

If it is any other value, it executes the default block of code and prints "Weekend" to the console.

반응형