{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Statements break continue goto return throw exception

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern