Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Statements (C# Programming Guide)
In this articleThe actions that a program takes are expressed in statements. Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition. The order in which statements are executed in a program is called the flow of control or flow of execution. The flow of control may vary every time that a program is run, depending on how the program reacts to input that it receives at run time. A statement can consist of a single line of code that ends in a semicolon, or a series of single-line statements in a block. A statement block is enclosed in {} brackets and can contain nested blocks. The following code shows two examples of single-line statements, and a multi-line statement block:
Types of statementsThe following table lists the various types of statements in C# and their associated keywords, with links to topics that include more information:
Declaration statementsThe following code shows examples of variable declarations with and without an initial assignment, and a constant declaration with the necessary initialization.
Expression statementsThe following code shows examples of expression statements, including assignment, object creation with assignment, and method invocation.
The empty statementThe following examples show two uses for an empty statement:
Embedded statementsSome statements, for example, iteration statements, always have an embedded statement that follows them. This embedded statement may be either a single statement or multiple statements enclosed by {} brackets in a statement block. Even single-line embedded statements can be enclosed in {} brackets, as shown in the following example:
An embedded statement that is not enclosed in {} brackets cannot be a declaration statement or a labeled statement. This is shown in the following example:
Put the embedded statement in a block to fix the error:
Nested statement blocksStatement blocks can be nested, as shown in the following code:
Unreachable statementsIf the compiler determines that the flow of control can never reach a particular statement under any circumstances, it will produce warning CS0162, as shown in the following example:
C# language specificationFor more information, see the Statements section of the C# language specification. See also
FeedbackSubmit and view feedback for What type of statement can be used to make a single alternative decision?Chapter 03 Key Terms Review. Is a term to indicate when multiple methods are written with the same name but with different parameter lists?Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.
What should you use if you wish to execute two or more statements conditionally group of answer choices?What should you use if you wish to execute two or more statements conditionally? A block enclosed within a pair of curly braces.
What type of label identifies a course of action in a switch structure?A case label identifies a course of action in a switch structure. Most switch structures contain several case labels. * The keyword break usually terminates a switch structure at the end of each case. Although other statements can end a case, break is the most commonly used.
|