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

C H A P T E R 9

Statements

What Are Statements?

Expression Statements

Flow-of-Control Statements

The if Statement

The if . . . else Statement

The switch Statement

The while Loop

The do Loop

The for Loop

Jump Statements

The break Statement

The continue Statement

Labeled Statements

The goto Statement

The using Statement

Other Statements

239

CHAPTER 9 STATEMENTS

What Are Statements?

The statements in C# are very similar to those of C and C++. This chapter covers the characteristics of a C# statement, as well as the flow-of-control statements provided by the language.

A statement is a source code instruction describing a type or telling the program to perform an action.

There are three major categories of statements:

Declaration statements: Statements that declare types or variables

Embedded statements: Statements that perform actions or manage flow of control

Labeled statements: Statements to which control can jump

Previous chapters have covered a number of different declaration statements, including declarations of local variables, classes, and class members. This chapter covers the embedded statements, which do not declare types, variables, or instances. Instead, they use expressions and flow-of-control constructs to work with the objects and variables that have been declared by the declaration statements.

A simple statement consists of an expression followed by a semicolon.

A block is a sequence of statements enclosed by matching curly braces. The enclosed statements can include the following:

Declaration statements

Embedded statements

Labeled statements

Nested blocks

The following code gives examples of each:

int x = 10;

// Simple declaration

int z;

// Simple declaration

{

// Block

int y = 20;

// Simple declaration

z = x + y;

// Embedded statement

top: y = 30;

// Labeled statement

...

 

{

// Nested block

...

 

}

 

}

 

240

CHAPTER 9 STATEMENTS

Note A block counts syntactically as a single embedded statement. Anywhere that an embedded statement is required syntactically, you can use a block.

An empty statement consists of just a semicolon. You can use an empty statement at any position where the syntax of the language requires an embedded statement but your program logic does not require any action.

For example, the following code is an example of using the empty statement:

The second line in the code is an empty statement. It is required because there must be an embedded statement between the if part and the else part of the construct.

The fourth line is a simple statement, as shown by the terminating semicolon.

if( x < y )

 

 

;

//

Empty statement

else

 

 

z = a + b;

//

Simple statement

Expression Statements

The previous chapter looked at expressions. Expressions return values, but they can also have side effects.

A side effect is an action that affects the state of the program.

Many expressions are evaluated only for their side effects.

You can create a statement from an expression by placing a statement terminator (semicolon) after it. Any value returned by the expression is discarded. For example, the following code shows an expression statement. It consists of the assignment expression (an assignment operator and two operands) followed by a semicolon. This does the following two things:

The expression assigns the value on the right of the operator to the memory location referenced by variable x. Although this is probably the main reason for the statement, this is considered the side effect.

After setting the value of x, the expression returns with the new value of x. But there is nothing to receive this return value, so it is ignored.

x = 10;

The whole reason for evaluating the expression is to achieve the side effect.

241

CHAPTER 9 STATEMENTS

Flow-of-Control Statements

C# provides the flow-of-control constructs common to modern programming languages.

Conditional execution executes or skips a section of code depending on a condition. The conditional execution statements are the following:

if

if...else

switch

Looping statements repeatedly execute a section of code. The looping statements are the following:

while

do

for

foreach

Jump statements change the flow of control from one section of code to a specific statement in another section of code. The jump statements are the following:

break

continue

return

goto

throw

Conditional execution and looping constructs (other than foreach) require a test expression, or condition, to determine where the program should continue execution.

Note Unlike C and C++, test expressions must return a value of type bool. Numbers do not have a Boolean interpretation in C#.

242

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