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](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXHZUwNxxS9YkWgj_v9KKTopuxXjnTrvLWZZHw_KkSfckp-S03EIoP4cUwiVlj_qH5LogFkmIujdGoZaX6RrxzV6YoVNSfkt9XL4au-A5v2ucSlKcr_o1XRqcYyPS-thK5hBK7J196RBSg/s400/new+project.png)
2. Then will appear the window New Project like the look below.
![C# SWITCH CASE STATEMENT](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp7PKCCQYZ5KsJr5dyzp1CGNXlXAq8h4RUl-rFfpWGT6KDlGLtew4f3sJSsw2i_cw8ytQCjwkgxUhRrzKExUew4R88VJcL7-zEf2jT-t5evhXvA8aLmg_8EToeOT1jrfLW_oHr5-WuM9ZH/s400/new+project2.png)
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](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigta7LDzrrUn4CIS2RPvqHo-PPlz54kYrXrKEXA9gxXEVshf8zVQmV4jzVc2CElyChUn2GTrf8K-_DfWMJKc4YGzR3v3f76QJRvIghcSWrWzIBpjBNqnzSsDIA5Hey8DWqOmpexdlSHy6-/s400/console.png)
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();
}
}
}
![C# SWITCH CASE STATEMENT](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPgV394FDvBQNld6Uiebmv0cW79BAR2Q-V3DWFbCxqrUx26hJBZEmomHTCSv2wKHBJUiabZZTtIhNYbBT42tjA1w4HrdHfXFIs78J23rC-n6owrNaoHW5msLtmoJ6EBm1OzrXjvjaalQx8/s400/console+switch+case.png)
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 i 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 i 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 i is equal to 3, then in the console program will show the text "Your choice of apple fruits".
No comments:
Post a Comment