The real nuts and bolts of a programming languages is that which controls the flow of a program called statements.

Types of Statements in C#

C# borrows most of its statements directly from C and C++, though there are some noteworthy additions and modifications.

  1. Expression & Control statements
  2. Labeled & goto statements

Control statements allow to branch the code depending on the certain condition against the value of an expression. There are many types of conditional statements.

  1. the if statement
  2. switch statement
  3. the for loop
  4. the while loop
  5. the do…while loop
  6. the foreach loop

C# provides number of statements which we can use to jump to another line in the program. i.e.

  1. the goto statement
  2. the break statement
  3. the continue statement
  4. the return statement

Statement lists and blocks

A statement list consists of one or more statements written in sequence, and a block permits multiple statements to be written in contexts where a single statement is expected. For instance, the example

shows two blocks. First is the Main function’s block and the other is the block
with the line Console.WriteLine(“Nested block”);

Labeled statements and goto statements

A labeled statement permits a statement to be prefixed by a label, and goto statements can be used to transfer control to a labeled statement. The example

is a convoluted version of the “Hello, world” program. The first statement transfers control to the statement labeled H. The first part of the message is written and then the next statement transfers control to the statement labeled W. The rest of the message is written, and the method returns.

Expression & Control statements

An expression statement evaluates a given expression. The value computed by the expression, if any, is discarded. Not all expressions are permitted as statements. In particular, expressions such as x + and x == 1 that have no side effects, but merely compute a value (which will be discarded), are not permitted as statements.

The if statement

An if statement selects a statement for execution based on the value of a Boolean expression. An if statement may optionally include an else clause that executes if the Boolean expression is false.
The example

shows a program that uses an if statement to write out two different messages depending on whether command line arguments were provided or not.

The switch statement

A switch statement executes the statements that are associated with the value of a given expression, or a default of statements if no match exists.
The example

switches on the number of arguments provided.

The while statement

A while statement conditionally executes a statement zero or more times ? as long as a Boolean test is true.

uses a while statement to find the first occurrence of a value in an array.

The do statement

A do statement conditionally executes a statement one or more times.
The example

reads from the console until the user types ?Exit? and presses the enter key.

The for statement

A for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly executes a statement and evaluates a sequence of iteration expressions.
The example

uses a for statement to write out the integer values 1 through 10.

The foreach statement

A foreach statement enumerates the elements of a collection, executing a statement for each element of the collection.
The example