Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

The switch Statement

Читайте также:
  1. A brief survey of control statements.
  2. A Decide which of these statements are true (T) or false (F).
  3. A. Decide whether each of the following statements is true or false. 1 страница
  4. A. Decide whether each of the following statements is true or false. 2 страница
  5. A. Decide whether each of the following statements is true or false. 3 страница
  6. A. Decide whether each of the following statements is true or false. 4 страница
  7. ACCOUNTING AND FINANCIAL STATEMENTS

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. Listing 3-2 shows how to use the switch statement with both int and string types.

Listing 3-2. Switch Statements: SwitchSelection.cs

using System;

class SwitchSelect
{
public static void Main()
{
string myInput;
int myInt;

begin:

Console.Write("Please enter a number between 1 and 3: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);

// switch with integer type
switch (myInt)
{
case 1:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 2:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 3:
Console.WriteLine("Your number is {0}.", myInt);
break;
default:
Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
break;
}

decide:

Console.Write("Type \"continue\" to go on or \"quit\" to stop: ");
myInput = Console.ReadLine();

// switch with string type
switch (myInput)
{
case "continue":
goto begin;
case "quit":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("Your input {0} is incorrect.", myInput);
goto decide;
}
}
}

Note: Listing 3-2 will throw an exception if you enter any value other than an int. i.e. the letter 'a' would be an error.

Listing 3-2 shows a couple of switch statements. The switch statement begins with the switch keyword followed by the switch expression. In the first switch statement in listing 3-2, the switch expression evaluates to an int type, as follows:

// switch with integer type
switch (myInt)
{
case 1:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 2:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 3:
Console.WriteLine("Your number is {0}.", myInt);
break;
default:
Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
break;
}

The switch block follows the switch expression, where one or more choices are evaluated for a possible match with the switch expression. Each choice is labeled with the case keyword, followed by an example that is of the same type as the switch expression and followed by a colon (:). In the example we have case 1:, case 2:, and case 3:. When the result evaluated in the switch expression matches one of these choices, the statements immediately following the matching choice are executed, up to and including a branching statement, which could be either a break, continue, goto, return, or throw statement. table 3-1 summarizes the branching statements.


Дата добавления: 2015-11-14; просмотров: 58 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Listing 3-1. forms of the if statement: IfSelection.cs| Table 3-1. C# Branching Statements

mybiblioteka.su - 2015-2024 год. (0.006 сек.)