HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

C# Conditional Statements
For branching statement execution we have 2 types of conditional statements in C#.
  • If..else
  • Switch
An Example:

Lets discuss the if statement with an example. In the example below if the user has not entered anything for his name, then the code in first if block is executed. To check if the user has entered his name we are using String.Empty field. This will determine if a string is empty.
If the user has enetered his name then the first if block is skipped. The control will not enter into the if block. The code in second if block will be executed.

using System;

class IfElseDemo

{

    public static void Main()

    {

        Console.WriteLine("Please enter your name"); // This message will be printed to the screen to prompt the user to enter their name

        string Name = Console.ReadLine();            // Read the name into a Local Variable

         

        if (Name == String.Empty)                    // If Name is empty then execute code in if block

        {

            Console.WriteLine("You have not entered your name");

        }

         

        if (Name != String.Empty)                    // If Name variable is not empty then execute code in if block

        {

            Console.WriteLine("Welcome " + Name);

        }

    }

}


The above example can also be written as shown below. Here we are using if..else instead of 2 if statements. In the first example as we are using 2 if statements, the if condition will be checked in both the if blocks, and when the condition evaluates to a boolean true, the code in the respective if block is executed else, the code will be skipped.

Here in the below example as we are using if..else, first the if condition will be checked and if the condition evaluates to a boolean true, then the code in if block is executed and the else block is skipped. On the other hand when the condition in the if block evaluates to a boolean false, the code in if block is skipped and the code in else block is executed.

using System;

class IfElseDemo

{

    public static void Main()

    {

        Console.WriteLine("Please enter your name");

        string Name = Console.ReadLine();           

         

        if (Name == String.Empty)                   

        {

            Console.WriteLine("You have not entered your name");

        }

        else

        {

            Console.WriteLine("Welcome " + Name);

        }

    }

}


A variation of if..else statement is shown in the below example. We have an if statement, followed by multiple else if statements and finally an else statement. This variation of if statement can be used to check multiple conditions.

using System;

class IfElseDemo

{

    public static void Main()

    {

        Console.WriteLine("Enter any number between 1 and 30");

        int Number = Convert.ToInt16(Console.ReadLine());

         

        if (Number ==10)                   

        {

            Console.WriteLine("Entered Number is 10");

        }

        else if (Number ==20)                   

        {

            Console.WriteLine("Entered Number is 20");

        }

        else if (Number ==30)

        {

            Console.WriteLine("Entered Number 30");

        }

        else

        {

            Console.WriteLine("Entered Number is not 10,20 or 30");

        }

       

    }

}


Another conditional statement in C# is the switch statement. Switch statement is especially useful if you want to check for multiple if conditions. To better understand switch statements lets look at an example. We will rewrite the above example that has multiple if statements using a switch statement and lets dissect what's going on
  • To the switch statement we have passed our variable Number of type int. Inside the switch code block we have series of case blocks. The case keyword is followed by an integers value (10,20 etc).
  • If the passed number variable has a value of 10 then, the statement in Case 10: block will be executed.
  • At the end of each case statement we have a break keyword. This keyword will break the execution and the control comes out of the loop. It is a compile time error in C# if you dont have the break statement.
  • Finally we have the default code block. This block will be executed only if none of the case blocks are executed. In our example if the passed in Number value is not 10,20 or 30 then non of case blocks will be executed and hence the default code block gets executed.

using System;

class SwitchCase

{

    public static void Main()

    {

        Console.WriteLine("Enter any number between 1 and 30");

        int Number = Convert.ToInt16(Console.ReadLine());

         

        switch (Number)

        {

            case 10:

                Console.WriteLine("Entered Number is 10");

                break;

            case 20:

                Console.WriteLine("Entered Number is 20");

                break;

            case 30:

                Console.WriteLine("Entered Number is 20");

                break;

            default:

                Console.WriteLine("Entered Number is not 10,20 or 30");

                break;

        }

    }

}

The above program can also be written as shown in the below example.
  • In this example we have Case 10 and Case 20 labels without any code in them followed by Case 30.
  • If the passed in Number value is 10,20 or 30 then the code in Case 30: block will be executed. This construct is especially useful when you want to check for multiple conditions and execute the same lines of code.
  • By placing case statements together, with no code in-between, you create a single case for multiple values. A case without any code will automatically fall through to the next case.

using System;

class CaseDemo

{

    public static void Main()

    {

        Console.WriteLine("Enter any number between 1 and 30");

        int Number = Convert.ToInt16(Console.ReadLine());

   

        switch (Number)

        {

            case 10:

            case 20:

            case 30:

                Console.WriteLine("Entered Number is {0}",Number);

                break;

            default:

                Console.WriteLine("Entered Number {0} is not 10,20 or 30",Number);

                break;

        }

    }

}

Another way to control the flow of logic in a switch statement is by using the goto statement and Labels as shown in the below example.
  • In this example we have 2 labels, Begin and Decide. Labels in a C# program are usually used to designate locations.
  • When the program executes "Enter any number between 1 and 30" is displayed to the user.
  • If the user has entered 10,20 or 30 then the code block following case 30: will be executed. The goto Decide: statement will cause the control to jump to the Decide: label and start executing from there again.

using System;

class CaseDemo

{

    public static void Main()

    {

    Begin: // This is a Label

 

        Console.WriteLine("Enter any number between 1 and 30");

        int Number = Convert.ToInt16(Console.ReadLine());

 

        switch (Number)

        {

            case 10:

            case 20:

            case 30:

                Console.WriteLine("Entered Number is {0}", Number);

                goto Decide;

            default:

                Console.WriteLine("Entered Number {0} is not 10,20 or 30", Number);

                goto Decide;

        }

 

    Decide:  //This is a Label

        Console.WriteLine("Do you want to continue? Yes or No");

        string choice = Console.ReadLine();

        switch (choice)

        {

            case "Yes":

            case "Y":

            case "y":

                goto Begin;

            case "No":

            case "N":

            case "n":

                Console.WriteLine("Program Terminated");

                break;

            default:

                Console.WriteLine("Wrong Choice");

                goto Decide;

        }

    }

}


Njoy Programming
   ByPrasad Cherukuri