using the switch statement and gives guidelines for good practice with switch. - CSharp Language Basics

CSharp examples for Language Basics:switch

Description

using the switch statement and gives guidelines for good practice with switch.

Demo Code

using System;/* w  w  w  . j  a v a2s  . c om*/
class Program
{
   static void Main(string[] args)
   {
      int unmarried = 0;
      int married = 1;
      int divorced = 2;
      int widowed = 3;
      int noneOfYourBusiness = 4;
      int maritalStatus = 3;
      switch (maritalStatus)
      {
         case 0:
         Console.WriteLine("Unmarried");
         break;
         case 1:
         Console.WriteLine("Married");
         break;
         case 2:
         case 3:
         Console.WriteLine("Divorced or Widowed");
         break;
         case 4:
         Console.WriteLine("None of your business, bub.");
         break;
         default:
         Console.WriteLine("Error: Marital status must be one of 0=unmarried, 1=married, 2=divorced, 3=widowed, 4=butt out");
         break;
      }
   }
}

Related Tutorials