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

Table 3-1. C# Branching Statements

Читайте также:
  1. A brief survey of control statements.
  2. A countable noun has a singular and a plural form.
  3. A Decide which of these statements are true (T) or false (F).
  4. A) Look at the table below and match the problem with its effect.
  5. A) Pronunciation drill. Pronounce the words, then look at the given map and fill in the table below.
  6. A) Summarize the information about the experiment in the table below.
  7. A. Decide whether each of the following statements is true or false. 1 страница
Branching statement Description
break Leaves the switch block
continue Leaves the switch block, skips remaining logic in enclosing loop, and goes back to loop condition to determine if loop should be executed again from the beginning.
goto Leaves the switch block and jumps directly to a label of the form "<labelname>:"
return Leaves the current method.
throw Throws an exception.

You may also include a default choice following all other choices. If none of the other choices match, then the default choice is taken and its statements are executed. Although use of the default label is optional, I highly recommend that you always include it. This will help catch unforeseen circumstances and make your programs more reliable.

Each case label must end with a branching statement, as described in table 3-1, which is normally the break statement. The break statement will cause the program to exit the switch statement and begin execution with the next statement after the switch block. There are two exceptions to this: adjacent case statements with no code in between or using a goto statement. Here's an example that shows how to combine case statements:

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

By placing case statements together, with no code in-between, you create a single case for multiple values. A case without any code will automatically fall through to the next case. The example above shows how the three cases for myInt equal to 1, 2, or 3, where case 1 and case 2 will fall through and execute code for case 3.

A case statement can only be an exact match and you can't use logical conditions. If you need to use logical conditions, you can use an if/else if/else statement.

Another way to control the flow of logic in a switch statement is by using the goto statement. You can either jump to another case statement, or jump out of the switch statement. The second switch statement in Listing 3-2 shows the use of the goto statement, as shown below:

// 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: in the current example, "continue", is a case of the switch statement -- not the keyword.

The goto statement causes program execution to jump to the label following the goto keyword. During execution, if the user types in "continue", the switch statement matches this input (a string type) with the case "continue": label and executes the "goto begin:" instruction. The program will then leave the switch statement and start executing the first program statement following the begin: label. This is effectively a loop, allowing you to execute the same code multiple times. The loop will end when the user types the string "quit". This will be evaluated with the case "quit": choice, which will print "Bye." to the console, break out of the switch statement and end the program.

Warning: You should not create loops like this. It is *bad* programming style. The only reason it is here is because I wanted to show you the syntax of the goto statement.

When neither the "continue" nor "quit" strings are entered, the "default:" case will be entered. It will print an error message to the console and then execute the goto decide: command. This will cause program execution to jump to the first statement following the decide: label, which will ask the user if they want to continue or quit. This is effectively another loop.

Clearly, the goto statement is powerful and can, under controlled circumstances, be useful. However, I must caution you strongly on its use. The goto statement has great potential for misuse. You could possibly create a very difficult program to debug and maintain. Imagine the spaghetti code that could be created by random goto statements throughout a program. In the next lesson, I'll show you a better way to create loops in your program.

 

VARIANTS

Objective – understand how to program single, dual alternative, and nested conditional structures; learn to rely on well-formed data for programs and be able to make one or two-way decisions with an if-statement.

 

Problem statement – solve 3 problems with the help of the conditional structures.

Task 1. Write a program that asks a user to enter coordinates and define whether this coordinate belong to the shaded area.

     
     
     
     
     
     
     
     
     
     

Task 2. Define variables x and a. Calculate the value of the expression Y with using if operator. Check all abnormal situations in which function does not exist.

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

 


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


<== предыдущая страница | следующая страница ==>
The switch Statement| Ending a formal letter

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