Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Console.WriteLine("Nice to see you, {0}!", fullName); } // end if

After the keyword else, you can place another if statement to check for another condition. In this type of structure, the program checks the first condition. If it’s true, the program executes the code after that condition and proceeds to the next line after the end of the entire if structure. If the initial condition is false, the program checks each succeeding condition. If none of the other conditions are true, the program executes the code following the else clause. You can use as many else if structures as you like, as long as you are careful to end each one with a right brace.

Trick Putting in a plain old else clause (without a condition) is a good idea even if you don’t think you will need it. The else clause is a great place to trap any unforeseen situations and respond to them.

Working with The Switch Statement

It’s relatively common to come across a situation in which you want to check one variable for a number of possible values. You can use the if...else if structure for these situations, but C# supplies a handy structure that specializes in these kinds of scenarios: the switch statement. In such situations, you use the switch statement, which I’ll explain a little later, but for now take a look at the Switch Demo program, which will ease you into switch statements.

The Switch Demo Program

Look at the following source code for an illustration of the switch statement:

using System;

namespace SwitchDemo

{

///<summary>

///Demonstrates use of the Switch structure

///Andy Harris, 11/10/01

///</summary>

class SwitchDemo

{

static void Main(string[] args)

{

string fullName; string greeting;

//get name from user

Console.Write("Please Enter your full name: "); fullName = Console.ReadLine();

//check name switch (fullName){

case "Bill Gates":

greeting = "Great job on C#"; break;

case "James Gosling":

greeting = "That Java thing is really cool"; break;

case "Alan Turing":

greeting = "The Turing machine was pretty amazing"; break;

45

case "Grace Hopper":

greeting = "Wow. You discovered the first computer bug!"; break;

default:

greeting = "We're waiting for your contribution to computer science, " + fullName;

break;

} // end switch

//write response Console.WriteLine(greeting); Console.WriteLine(); Console.WriteLine();

Console.WriteLine("Press \"enter\" to continue"); Console.ReadLine();

} // end main } // end class

}// end namespace

Examining How Switch Statements Work

The switch statement looks at one variable or expression (in this case, the variable fullName) and compares it to several cases. In essence, this code

switch (fullName){

case "Bill Gates":

greeting = "Great job on C#"; break;

is equivalent to the following code, which contains a switch statement:

if (fullName == "Bill Gates")

{

greeting = "Great job on C#";

}

The variable you are comparing belongs in a pair of parentheses right after the keyword switch. The rest of the structure goes inside a pair of braces. For each value you want to compare, you build a case structure. This structure describes the value you are comparing the variable to. For example, "Bill Gates" is one possible value of userName, and "James Gosling" is another, so each of these terms makes up a case. The case structure begins with the keyword case, followed by the value you want to compare and then a colon (:) character. You must end each case with a break statement, which informs the computer that you are finished considering this possible value for the expression. The break structure helps the computer understand that you are done writing code that should happen if the user is Bill Gates, for example, and you’re ready to start the next case (which might be James Gosling).

Trap If you are familiar with another programming language, take a careful look at the switch statement. In C#, the switch statement differs from its cousins in the other popular languages. It is possible to switch on a string variable (this is impossible in C), and C# requires the break statement at the end of each case, unlike Visual Basic or C.

The switch statement has a section named default:, which acts like the else clause in an if structure. If none of the other cases turn out to be true, the code in the else clause executes. It’s a good idea to include a default clause in any switch statement you build.

Trick

46

Соседние файлы в предмете Программирование на C++