Statement

Description

A statement is one complete C# command. The end of the command is indicated with a semi-colon. You could consider the semi-colon the period of the sentence.

One or more statements can be grouped into an atomic “block” by enclosing the statements in curly braces { and }. This is commonly seen after an if or a loop statement (while, for, etc.). The closing curly brace } acts as the semi-colon for the entire block.

Syntax

C# command;

or

{

statement

[ statement ]

}

Example

total = costOfItems + taxAmount;

or

{

taxAmount = costOfItems * 0.087;

total = costOfItems + taxAmount;

}

If statement

Description

The if allows you to make a decision, answering a true/false question. The bool-expression is the question being asked.

  • If the bool-expression evaluates as true, then the immediately following statement is executed.
  • If the bool-expression evaluates to false then:
  • If there are following else if statements, then they will be evaluated in order until one is found to be true or all are evaluated to be false.
  • If there is a trailing else, then the associated statement will be executed. There can be at most one else. The else is the default path, executed only if all associated if / else ifs evaluate as false.

Note there is no semi-colon after the closing parenthesis, i.e. at the end of the if statement line itself.

Syntax

if (bool-expression)

statement

[ else if (bool-expression)

statement ]

[ else

statement ]

Example (display a message based on the temperature)

if ( temp < 0 )

Console.WriteLine(“It is below zero outside!”);

else if ( temp < 32 )

Console.WriteLine(“It is freezing!”);

else if ( temp < 60 )

Console.WriteLine(“A bit chilly, wear a coat!”);

else

Console.WriteLine(“It is warm enough, no coat required!”);s

Switch statement

Description

The switch is an alternate form of the if. It is used when each condition check is for a specific value. It only works if you are matching for exact values; it cannot be used to do less-than numeric checks for instance.

Think of the switch statement as a multi-step dial switch that can only select certain settings.

With the switch you are “switching” through the possible values a variable can hold. The variable is identified in the expression. The possible values (constant-expression) are the “cases” to consider and are identified by the case statements. Note that the case statements end with a colon.

Each case statement compares a specific value with the variable being examined. If the variable value matches the case value the statement (or block of statements) associated with the case is executed.

Each case must end with a break.

You can provide a default case by simply providing a final case named default (without a

Syntax

switch ( expression )

{

case value:

statement

break;

[default:

statement

break;]

}

Example (process simple math operator for a calculator program)

switch ( mathOpString )

{

case “+”:

Console.WriteLine(num1 + num2);

break;

case “-”:

Console.WriteLine(num1 - num2);

break;

case “*”:

Console.WriteLine(num1 * num2);

break;

case “/”:

Console.WriteLine(num1 / num2);

break;

default:

Console.WriteLine(“Invalid input!”);

break;

}

While loop statement

Description

The while is the most basic loop. It means “keep looping as long as (while) the stated condition (bool-expression) is true.

The bool-expression is where the loop-control variable is checked for loop termination. The loop-control variable must be initialized before the while statement.

The statement is often called the “body” of the loop.

The loop control variable is changed in the body of the loop.

Syntax

while (bool-expression)

statement

Example (require the user to enter something/anything as their name)

String userName = “”;

while ( userName == “” )

{

Console.Write(“Enter your name: “);

userName = Console.ReadLine();

}

For loop statement

Description

The for loop is a general loop that combines the three required loop control elements into the statement itself:

  1. initialize the loop-control variables (initializers)
  2. check the loop-control variables for loop termination (bool-expression)
  3. iterate (or change) the loop-control variables (iterators)

For loops with more than one loop-control variable, each can be handled in the for statement. For instance, they can all be initialized in the initializer section, each separated by a comma.

Syntax

for (initializers;bool-expression ; iterators)

statement

Example 1 (single loop-control variable: display the 1st 5 elements in a list)

for ( int position = 0 ; position < list.Count ; position++ )

{

Console.WriteLine(“The next list element is: ” + list[position]);

}

Example 2 (two loop-control variables: at most 5 tries to enter name)

int counter;

String userName;

for (counter = 0, userName = “” ; counter <= 5 & userName == “” ; counter++ )

{

Console.Write(“Enter your name: ”);

userName = Console.ReadLine();

}

Foreach loop statement

Description

The foreach loop allows you to iterate through all the items in a list or collection, examining each item from the list/collection one at a time.

It is intended to be used when you want to examine or use all the items in a list.

The loop-control variable is completely handled by the foreach statement itself. It is initialized, checked and changed for you.

Syntax

foreach ( var-type var-name incollection)

statement

Example (display all the names of a list of names )

foreach ( String name in nameList )

{

Console.WriteLine(“Name: ” + name);

}

Here the loop-control variable is “name” and its type is “String”. The collection/list is “nameList”.

Each iteration (i.e. each time through) the loop, the foreach determines if there are more items in the list/collection. If there are more, the loop-control variable is updated with the next item and the loop body is executed. When the foreach determines there are no more items in the list/collection, it stops/exits the loop.