Statements

Functions comprise statements that execute sequentially in the textual order in which they appear. A statement block is a series of statements appearing between braces (the {} tokens).

A declaration statement declares a new variable, optionally initializing the variable with an expression. A declaration statement ends in a semicolon. You may declare multiple variables of the same type in a comma-separated list. For example:

bool rich = true, famous = false;

A constant declaration is like a variable declaration, except that the variable cannot be changed after it has been declared, and the initialization must occur with the declaration:

const double c = 2.99792458E08;

Expression statements are expressions that are also valid statements. In practice, this means expressions that “do” something; in other words, expressions that:

Expressions that do none of these are not valid statements:

string s = "foo";
s.Length;          // Illegal statement: does nothing!

When you call a constructor or a method that returns a value, you’re not obliged to use the result. However, unless the constructor or method changes state, the statement is useless:

new StringBuilder();     // Legal, but useless
x.Equals (y);            // Legal, but useless

Selection statements conditionally control the flow of program execution.

C# enables a sequence of statements to execute repeatedly with the while, do-while, for and foreach statements.

The C# jump statements are break, continue, goto, return, and throw. We cover the throw keyword in try Statements and Exceptions.