I l @ ve RuBoard |
As a derivation of C and C++, C# borrows most of its statements from these two languages. The next section outlines the statements found in C# and their usage. BlocksA block statement allows multiple statements to be written in contexts where a single statement is allowed. A block statement consists of a statement list enclosed by braces: \ static void main { } Statement ListsA statement list is simply one or more statements written in sequence and enclosed in blocks as outlined in the preceding section: static void main { refresh(); repaint(); } Empty StatementAn empty statement performs no operations and is used where a statement is required, as in this while loop with a null body: while (x > 0) { ; } LabeledA labeled statement allows a statement to be referenced by a goto statement. The scope of the label is the block that encloses it, including any nested blocks: static void main { if(x>0) goto MyLabel; x++; MyLabel: return x; } DeclarationsA declaration statement declares a local variable or constant: int x = 3; const decimal pi = 3.14; ExpressionsAs expected, an expression statement evaluates a given statement: return (x * y); repaint(this); SelectionSelection statements allow for the execution of one of a list of possible statements based upon the value of a controlling expression. Selection statements include the if and switch statements. ifThe if statement selects a statement for execution based on a Boolean expression: if (x > 0) { x--; } This statement can be coupled with an else clause to specify a statement for execution if the condition fails: if (x > 0) { x--; } else { return x; } if statements can also be nested: if (x !=3) { if (x > 0) { x--; } else { return x; } } switchTo provide multiple statements for possible execution based upon the value of the condition expression, we use the switch statement: switch (x) { case 1: Console.WriteLine("X = 1"); break; case 2: Console.WriteLine("X = 2"); break; case 3: Console.WriteLine("X = 3"); break; default: Console.WriteLine("X does not equal 1,2, or 3"); break; } The switch statement evaluates the expression and looks for a match with a constant value from one of the labeled statements. Upon finding a match, execution control is transferred to the statement list for the matching label. If no match is found, control is transferred to the default label and its statement list. Note that the statement list of the switch statement must end in a construct that renders the end point of the statement list unreachable, usually break . IterationIteration statements allow for the execution of a statement list as long as some expression evaluates true . C# includes four such iteration statements: while , do , for , and foreach . whileThe while statement executes a statement list zero or more times, depending on the value of the conditional expression: while (x < 100) { Console.WriteLine(x); x++ } doSimilar to the while statement, the do statement executes one or more times: do { Console.WriteLine(x); x++ } while(x < 100) forThe for statement evaluates a series of initialization expressions and repeats execution based on evaluation of these expressions. In addition to the statement list to be executed, the for statement has three parts : initializer , condition , and iterator . for (int x = 0; x < 100; x++) { Console.WriteLine(x); } foreachVery similar to the for statement, the foreach statement enumerates a collection and executes the statement list once for each item in the collection: foreach (int x in args) { Console.WriteLine(x); } jumpAnother type of statement, the jump statement, allows the unconditional transfer of control execution in a C# program. Jump statements include break , continue , goto , return , and throw statements. breakThe break statement unconditionally exits the nearest enclosing switch , while , do , for , or foreach statement: switch (x) { case 1: Console.WriteLine("X = 1"); break; default: Console.WriteLine("X does not equal 1"); break; } continueThe continue statement restarts the nearest enclosing while , do , for , or foreach statement: while(true) Console.WriteLine(x); if(x < 100) continue; break; } gotoAs mentioned during the discussion of labeled statements, the goto statement transfers control to a labeled statement: static void main { if(x>0) goto MyLabel; x++; MyLabel: return x; } returnThe return statement returns control to the caller of the function in which the return statement appears: int GetStudentID(string FullName) { return x; } throwThe throw statement throws an exception. See the try statement for usage. tryThe try statement provides exception handling during execution of a statement block. The try block may be followed by one or more catch blocks and may be followed by one finally block: try { repaint(); } catch (Exception e) { //handle gracefully if(x < 0) // out of range { throw new Exception("Index out of Range"); } } finally { // any clean-up code here } checked and uncheckedThe checked and unchecked statements are used to control the overflow checking context for integral-type arithmetic operations and conversions: int x = 1000000; int y = 1000000; Console.WriteLine(unchecked(x^2)); Console.WriteLine(checked(y^2)); lockThe lock statement obtains the mutual-exclusion lock for a given object, executes a statement list, and then releases the lock: lock(MyResource) { MyResource.Name = "Bob"; } usingThe using statement obtains one or more resources, executes a statement list, and then disposes of the resource: using (MyResource r = new MyResource()) { r.Update(); |
I l @ ve RuBoard |