Conditional Statements (if, else, switch) in c#

Conditional Statements (if, else, switch) in c#

Conditional statements in the C# programming language help change the default flow structure of a program by making decisions based on a particular program logic expression condition. Popular conditional constructs used in C# programming include the if, else if, else statement, and switch block statement features.

Conditional Statements (if, else, switch) in c#

If, else if, and else statement concepts in the C# programming language.

A user-defined if statement in a C# program first evaluates or analyzes a program condition expression. If the user-defined if condition expression is true, the if statement executes the corresponding program source code block. Otherwise, it executes a block of code containing the else statement in the current program. If the if condition expression is false, the else if statement tests multiple available conditions one after the other until a block in the current program returns true or false.

If, else if, and else statement syntax in C#.

if(condition)

{

// related program Code will be executed, when if condition is true

}

else if (nextCondition)

{

// here it executes program Code, when if next condition is true

}

otherwise

{

// here it runs else statement Code, when above if all conditions are in false state

}

Example of if, else if, and else statements in C#.

Using System;

class IfElseifElseSt

{

static void Main()

{

int integer = 1;

if (integer > 0)

{

Console.WriteLine(“Positive integer value found”);

}

else if (integer < 0)

{

Console.WriteLine(“Negative integer value found”);

}

otherwise

{

Console.WriteLine(“Zero value found”);

}

}

}

Switch Statement in C# Programming.

In the C# programming language, the switch statement is used to find or test multiple possible potential values ​​for a user-defined, declared program variable. A switch statement is an efficient statement that can be read in a block format, allowing for multiple user-defined program expression conditions in an easier order than an if-else statement chain.

Switch Statement in C# syntax.

switch (expression)

{

case test 1:

// here’s the program source code to be executed if expression is == test1

break;

case test 2:

// here’s the program source code to be executed if expression == test2

break;

case test 3:

// here’s the program source code to be executed if expression == test3

break;

default:

// default statement only runs when all above statements are false or if none of the above source code matches

break;

}

Switch Statement example in C#.

Using System;

class SwitchStatement

{

static void Main()

{

int car = 2;

switch (car)

{

case 1:

Console.WriteLine(“Toyota Fortuner”);

break;

case 2:

Console.WriteLine(“Tata Punch”);

break;

case 3:

Console.WriteLine(“Hyundai Creta”);

break;

case 4:

Console.WriteLine(“Maruti Suzuki Dzire”);

break;

case 5:

Console.WriteLine(“Mahindra Scorpio”);

break;

default:

Console.WriteLine(“Invalid car selection”);

break;

}

}

}

Switch Expression Statement/C# 8.0+ in C#.

C# 8.0 introduced a shorter switch expression syntax, allowing C# programmers to use a shorter but more effective switch statement in the new switch block statement, similar to the long-length switch statement (if statement).

using System;

string courseName = course switch

{

1 => “C# Programming”,

2 => “Java”,

3 => “Python”,

4 => “Javascript”,

5 => “Matlab”,

_ => “Invalid course choice” // here it is the default case

};

Console.WriteLine(courseName);

Group selection of If Else if, Else statements with Switch statement example in C# program.

using System;

class IfslesifelseWithSwitch

{

    static void Main()

    {

        // here we use single IF statement

        int integer = 1;

        if (integer > 0)

        {

            Console.WriteLine(“checked value is positive”);

        }

        // here we use IF with ELSE statement

        int age =22;

        if (age >= 21)

        {

            Console.WriteLine(“You are mature”);

        }

        else

        {

            Console.WriteLine(“You are not mature”);

        }

        // here we use IF ELSE IF with ELSE statement

        int total = 91;

        if (total >= 90 && total <=100)

        {

            Console.WriteLine(“Grade A”);

        }

        else if (total >= 70 && total <= 85)

        {

            Console.WriteLine(“Grade B”);

        }

        else if (total >= 50 && total <= 65)

        {

            Console.WriteLine(“Grade C”);

        }

        else

        {

            Console.WriteLine(“Fail”);

        }

        // here we use SWITCH statement

        int laptop = 4;

        switch (laptop)

        {

            case 1:

                Console.WriteLine(“Macbok Pro”);

                break;

            case 2:

                Console.WriteLine(“Hp Pavilion”);

                break;

            case 3:

                Console.WriteLine(“Dell Alienware”);

                break;

            case 4:

                Console.WriteLine(“Lenovo Idepad”);

                break;

            case 5:

                Console.WriteLine(“Asus Rog”);

                break;

            default:

                Console.WriteLine(“Invalid Laptop Selection”);

                break;

        }

    }

}

Some special features to remember in C#.

  • Use the if-else block statement for more complex programming logic structures in C# programs.
  • The if-else statement is a better choice for highly complex program expressions or range-based conditions.

Use the switch statement for fixed values ​​​​in C# programming.

Using a switch statement is a better choice when checking or testing a user-defined program variable against multiple individual values ​​in a C# program.

Always use a break in a switch statement.

Not using the break keyword in a switch statement can cause the program to develop “fall-through” logic, which will cause your switch statement to continue executing without stopping.

Switch statement default block.

In C# programs, both if-else and switch block statements are essential for a default or catch-all condition. The default statement is run when all condition expressions in both of these returns false.

Leave a Reply