Statements break continue goto return throw exception

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e terminated with a semicolon (;) Just like C, C++ and Java Block statements { ... } don’t need a semicolon Statements Syntax Comments // Comment a single line, C++ style /* Comment multiple lines, C style */ Statements Statement Lists & Block Statements Statement list: one or more statements in sequence Block statement: a statement list delimited by braces { ... } static void Main() { F(); G(); { // Start block H(); ; // Empty statement I(); } // End block } Statements Variables and Constants static void Main() { const float pi = 3.14f; const int r = 123; Console.WriteLine(pi * r * r); int a; int b = 2, c = 3; a = 1; Console.WriteLine(a + b + c); } Statements Variables and Constants The scope of a variable or constant runs from the point of declaration to the end of the enclosing block Statements Variables and Constants Within the scope of a variable or constant it is an error to declare another variable or constant with the same name { int x; { int x; } } // Error: can’t hide variable x Statements Variables Variables must be assigned a value before they can be used Explicitly or automatically Called definite assignment Automatic assignment occurs for static fields, class instance fields and array elements void Foo() { string s; Console.WriteLine(s); } // Error Statements Labeled Statements & goto goto can be used to transfer control within or out of a block, but not into a nested block static void Find(int value, int[,] values, out int row, out int col) { int i, j; for (i = 0; i < values.GetLength(0); i++) for (j = 0; j < values.GetLength(1); j++) if (values[i, j] == value) goto found; throw new InvalidOperationException(“Not found"); found: row = i; col = j; } Statements Expression Statements Statements must do work Assignment, method call, ++, --, new static void Main() { int a, b = 2, c = 3; a = b + c; a++; MyClass.Foo(a,b,c); Console.WriteLine(a + b + c); a == 2; // ERROR! } Statements if Statement Requires bool expression int Test(int a, int b) { if (a > b) return 1; else if (a < b) return -1; else return 0; } Statements switch Statement Can branch on any predefined type (including string) or enum User-defined types can provide implicit conversion to these types Must explicitly state how to end case With break, goto case, goto label, return, throw or continue Eliminates fall-through bugs Not needed if no code supplied after the label Statements switch Statement int Test(string label) { int result; switch(label) { case null: “runnergoto case “runner-up”; case “winner”: result = 1; break; “runnercase “runner-up”: result = 2; break; default: result = 0; } return result; } Statements while Statement Requires bool expression int i = 0; while (i < 5) { ... i++; int i = 0; } do { ... i++; } while (i < 5); while (true) { ... } Statements for Statement for (int i=0; i < 5; i++) { ... } for (;;) { ... } Statements foreach Statement Iteration of arrays public static void Main(string args) { foreach (string s in args) Console.WriteLine(s); } Statements foreach Statement Iteration of user-defined...
View Full Document

This note was uploaded on 04/04/2014 for the course CS 5950 taught by Professor Staff during the Summer '08 term at Western Michigan.

Ask a homework question - tutors are online