Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 9 STATEMENTS

The goto Statement

The goto statement unconditionally transfers control to a labeled statement. Its general form is the following, where Identifier is the identifier of a labeled statement:

goto Identifier ;

For example, the following code shows the simple use of a goto statement:

bool thingsAreFine; while (true)

{

thingsAreFine = GetNuclearReactorCondition();

if ( thingsAreFine ) Console.WriteLine("Things are fine.");

else

goto NotSoGood;

}

NotSoGood: Console.WriteLine("We have a problem.");

The goto statement must be within the scope of the labeled statement.

A goto statement can jump to any labeled statement within its own block or can jump out to any block in which it is nested.

A goto statement cannot jump into any blocks nested within its own block.

Caution Using the goto statement is strongly discouraged, because it can lead to code that is poorly structured and difficult to debug and maintain. Edsger Dijkstra’s 1968 letter to the Communications of the ACM, entitled “Go To Statement Considered Harmful,” was an important contribution to computer science; it was one of the first published descriptions of the pitfalls of using the goto statement.

The goto Statement Inside a switch Statement

There are also two other forms of the goto statement, for use inside switch statements. These goto statements transfer control to the correspondingly named switch label in the switch statement.

goto case ConstantExpression; goto default;

261

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]