Sunday, August 23, 2009

Labeled statements and goto statements

1.7.2 Labeled statements and goto statements
A labeled statement permits a statement to be prefixed by a label, and goto statements can be used to transfer control to a labeled statement.
The example
using System;
class Test
{
static void Main() {
goto H;
W: Console.WriteLine("world");
return;
H: Console.Write("Hello, ");
goto W;
}
}
is a convoluted version of the “Hello, world” program. The first statement transfers control to the statement labeled H. The first part of the message is written and then the next statement transfers control to the statement labeled W. The rest of the message is written, and the method returns.
1.7.3 Local declarations of constants and variables
A local constant declaration declares one or more local constants, and a local variable declaration declares one or more local variables.
The example
class Test
{
static void Main() {
const int a = 1;
const int b = 2, c = 3;
int d;
int e, f;
int g = 4, h = 5;
d = 4;
e = 5;
f = 6;
}
}
shows a variety of local constant and variable declarations.

1.7.4 Expression statements
An expression statement evaluates a given expression. The value computed by the expression, if any, is discarded. Not all expressions are permitted as statements. In particular, expressions such as x + y and x == 1 that have no side effects, but merely compute a value (which will be discarded), are not permitted as statements.
The example
using System;
class Test
{
static int F() {
Console.WriteLine("Test.F");
return 0;
}
static void Main() {
F();
}
}
shows an expression statement. The call to the function F made from Main constitutes an expression statement. The value that F returns is simply discarded.

No comments: