4.3.The switch Statement

Another form of selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter. The types of the values a switch statement operates on can be booleans, enums, integral types, and strings. Lesson 2: Operators, Types, and Variables discussed the bool type, integral types and strings and Lesson 17: Enums will teach you what an enum type is. Listing 3-2 shows how to use the switch statement with both int and string types.
Listing 3-1. Switch Statements:

using System;

namespace CSharp
{
         class switch_Selection
         {
                 static void Main(string[] args)
                 {
                         int day;
                         again:
                         Console.Write("Enter Number of Day: ");
                         day = int.Parse(Console.ReadLine());
                         switch (day)
                         {
                                 case 1:        Console.WriteLine("That's Monday");
                                                      break;
                                 case 2:        Console.WriteLine("That's Tuesday");
                                                      break;
                                 case 3:        Console.WriteLine("That's Wednesday");
                                                      break;
                                 case 4:        Console.WriteLine("That's Thursday");
                                                      break;
                                 case 5:        Console.WriteLine("That's Friday");
                                                      break;
                                 case 6:        Console.WriteLine("That's Saturday");
                                                      break;
                                 case 7:        Console.WriteLine("That's Sunday");
                                                      break;
                                 default:       Console.WriteLine("Your input allowd 1 to 7 only, Please try again");
                                                      goto again;
                         }
                         Console.ReadKey();
                 }
         }
}

Listing 3-2. Switch Statements:

using System;

namespace CSharp
{
        class switch_Selection1
        {
                static void Main(string[] args)
                {
                        char symbol;
                        double v1, v2, result;
                        top:
                        Console.Write("Enter V1: ");
                        v1 = double.Parse(Console.ReadLine());
                        again:
                        Console.Write("Enter V2: ");
                        v2 = double.Parse(Console.ReadLine());
                        Console.WriteLine("You can use calculate symbol like: +,-,*,/");
                        Console.Write("Enter calculate symbol: ");
                        symbol = Console.ReadKey().KeyChar;
                        Console.WriteLine();
                        switch(symbol)
                        {
                                case '+':        result = v1 + v2;        break;
                                case '-':        result = v1 - v2;        break;
                                case '*':        result = v1 * v2;        break;
                                case '/':
                                                      if(v2==0)
                                                      {
                                                              Console.WriteLine("Can not Divided by zero, Please try again!");
                                                              goto again;
                                                      }
                                                      else
                                                      {
                                                              result = v1 / v2;
                                                      }
                                                      break;
                                default: Console.WriteLine("That's allowed only 4 symbols like + - * / only, Please try again!");
                                goto top;

                        }
                        Console.WriteLine("Result of Calculate is: {0}", result);
                        Console.ReadKey();
                }
        }
}

Example 1: Windows Forms Application: 

Yes No Suggest edit
Suggest Edit