19. Statements#

If we think of code as language, then statements in C# are like complete sentences. In C#, a statement is the smallest standalone element of the language that expresses some action to be carried out. It is an instruction written in the source code for execution.

Just like our previous jar metaphor, think of a statement as a step-by-step instruction in a recipe. For example, “Put the marble in the jar,” or “Remove a marble from the jar.” It represents an action or a sequence of actions.

For instance, when you write Console.WriteLine("Hello, World!"); you are creating a statement – one that instructs the program to output the string "Hello, World!" to the console.

../_images/cover-statements.jpg

Fig. 19.1 A sequence of statements is executed like the steps on a factory assembly line, or crossed off one by one like the items on a todo list.#

There are many different types of statements in C#. Basic ones include:

  • Declaration statements

  • Assignment statements

  • Expression statements

  • Block statements

A declaration statement introduces a new variable or constant. Here is an example:

int numberOfMarbles;

In this statement, we are declaring a new variable named numberOfMarbles of type int.

An assignment statement assigns a value to a variable. For instance:

numberOfMarbles = 5;

This statement assigns the value 5 to the variable numberOfMarbles.

An expression statement is made up of an expression. For instance:

Console.WriteLine(numberOfMarbles);

This statement prints out the value of numberOfMarbles to the console. It is an expression statement not because numberOfMarbles is an expression (which it is) but because we’re invoking the method WriteLine and then terminating the line with semicolon (;). We’ll talk more about methods later.

An expression statement is a type of statement that contains an expression. But not all expressions can be used as expression statements. In C#, the types of expressions that can also be used as statements include method calls, object creation expressions using the new keyword (both of which we’ll talk about later), and assignments. For instance, the assignment expression a = 5 can be used as an expression statement by adding a semicolon to the end a = 5;. Remember, while all expression statements contain expressions, not all expressions can serve as statements on their own. Expressions used in this way perform an action and often change the state of the program, like modifying a variable or outputting a value.

Finally, a block statement, or a block, is a group of statements that are executed sequentially. It is written between opening and closing curly brackets { }.

{
    int numberOfMarbles;
    numberOfMarbles = 5;
    Console.WriteLine(numberOfMarbles);
}

In this block, three statements are executed sequentially - a declaration, an assignment, and an expression statement. Block statements are used in selection and iteration statements which we will delve into in upcoming chapters.

Statements structure and control the flow of our code. They allow us to execute actions and are fundamental to creating meaningful programs.