Saturday, 15 April 2017

C# SWITCH CASE STATEMENT ~ mkniit

C# SWITCH CASE STATEMENT

Switch Case statement is also one conditioned statements. Pernyatan Switch Case is basically almost the same as the if then else. If you require a statement with many conditions, use the Switch Case statement. For more mamahami about the Switch Case statement, follow the following steps-steps:


1. Create a new application project. In Visual Studio, on the menu click File> New  >Project. For more details, see the following menu on the display.
  
C# SWITCH CASE STATEMENT



2. Then will appear the window New Project like the look below.



C# SWITCH CASE STATEMENT



3. Write down the name of the project that will be created on a field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.


C# SWITCH CASE STATEMENT


4. Then you will have created the framework of main program listings by Visual Studio. Here's how it looks.

5. Next step, write the following program listing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine("Enter your choice (1-3): ");
            string choice = Console.ReadLine();
            int i = Convert.ToInt16(choice);
            switch (i)
            {
                case 1:
                    Console.WriteLine(" Your choice banana fruits");
                    break;
                case 2:
                    Console.WriteLine(" Your choice watermelon fruits");
                    break;
                case 3:
                    Console.WriteLine(" Your choice apple fruits");
                    break;
            }

            Console.ReadLine();
        }
    }
}
6. After you write down the program listings, press the F5 key to run the program.
 
C# SWITCH CASE STATEMENT

Who becomes a statement now is why at the moment of entering the figures display appears 1 "Your choicebanana fruits"? For more details, look at the piece listings the following programs:
            Console.WriteLine("Enter your choice (1-3): ");
            string choice = Console.ReadLine();
            int i = Convert.ToInt16(choice);
            switch (i)
            {
                case 1:
                    Console.WriteLine(" Your choice banana fruits");
                    break;
                case 2:
                    Console.WriteLine(" Your choice watermelon fruits");
                    break;
                case 3:
                    Console.WriteLine(" Your choice apple fruits");
                    break;
            }

Explanation per line is:
  • Console.WriteLine("Enter your choice(1-3):"); the display is used to display the text "Enter your choice (1-3): ";.
  • string choice=Console.ReadLine();. Used to store results of plugging the keyboard into a string variable named options.
  • int i =convert.ToInt16(choice); used to change the data contained in the string variable into integer data type,which is then stored into a variable i.
  • switch (i). is used to express the value of initial conditions.
  • case 1: Console. WriteLine ("Your choice banana fruits"); break;used for checking the condition of the if the value of i is equal to 1.If the value of is equal to 1,then in the console program will show the text "Your choice banana fruits".
  • case 2: Console. WriteLine ("Your choice watermelon fruits"); break; used for checking the condition of the if the value of is equal to 2.If the value of i is equal to 2, then in the console program will show the text "Your choice watermelon fruits".
  • case 3: Console. WriteLine ("Your choice of apple fruits"); break; used for checking the condition of the if the value of i is equal to3.If the value of is equal to 3, then in the console program will show the text "Your choice of apple fruits". 

No comments:

Post a Comment