8.6 Statements

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

8.6 Statements

C# borrows most of its statements directly from C and C++, though there are

some noteworthy additions and

modifications. The table below lists the kinds of statements that can be

used, and provides an example for

each.

Statement Example

Statement lists and block

statements

static void Main() {

F();

G();

{

H();

I();

}

}

Labeled statements and goto

statements

static void Main(string[] args) {

if (args.Length == 0)

goto done;

Console.WriteLine(args.Length);

done:

Console.WriteLine("Done");

}

Local constant declarations static void Main() {

const float pi = 3.14f;

const int r = 123;

Console.WriteLine(pi * r * r);

}

Local variable declarations static void Main() {

int a;

int b = 2, c = 3;

a = 1;

Console.WriteLine(a + b + c);

}

Expression statements static int F(int a, int b) {

return a + b;

}

static void Main() {

F(1, 2); // Expression statement

}

if statements static void Main(string[] args) {

if (args.Length == 0)

Console.WriteLine("No args");

else

Console.WriteLine("Args");

}

switch statements static void Main(string[] args) {

switch (args.Length) {

case 0:

Console.WriteLine("No args");

break;

case 1:

Console.WriteLine("One arg ");

break;

default:

int n = args.Length;

Console.WriteLine("{0} args", n);

break;

}

}

while statements static void Main(string[] args) {

int i = 0;

while (i < args.Length) {

Console.WriteLine(args[i]);

i++;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航