Like C++ it contains three types of statements.
1. Simple Statements
2. Compound Statements
3. Control Statements
We’ll be covering the following topics in this tutorial:
Simple Statements
A simple statement is any expression that terminates with a semicolon. For example
varl = var2 + var3;
var3 = varl++;
var3++;
Compound Statements:
Related statements can be grouped together in braces to form a compound statement or block. For example:
{
int i = 4;
Console.WriteLine (i);
i++;
}
Semantically, a block behave like a statement and can be used anywhere a single statement is allowed. There is no semicolon after the closing braces. Any variable declared in a block remain in scope up to the closing brace. Once the block is exited, the block variables cease to exist. Blocking improves readability of program code and can help make your program easier to control and debug.
Control Statements
Again control statements are divided into three statements
(a) Loop statements
(b) Jump statements
(c) Selection statements
(a) Loop Statements
C# provides a number of the common loop statements:
• while
• do-while
• for
• foreach
while loops
Syntax: while (expression) statement[s]
A ‘while’ loop executes a statement, or a block of statements wrapped in curly braces, repeatedly until the condition specified by the Boolean expression returns false. For instance, the following code.
int a =0;
While (a < 3) {
System.Console. WriteLine (a);
a++;
}
Produces the following output:
0
1
2
do-while loops
Syntax: do statement [s] while (expression)
A ‘do-while’ loop is just like a ‘while’ loop except that the condition is evaluated after the block of code specified in the ‘do’ clause has been run. So even where the condition is initially false, the block runs once. For instance, the following code outputs ‘4’:
Int a = 4;
do
{
System.Console. WriteLine (a);
a++;
} while (a < 3);
for loops
Syntax: for (statement1; expression; statement2) statement[s]3
The ‘for’ clause contains three part. Statementl is executed before the loop is entered.
The loop which is then executed corresponds to the following ‘while’ loop:
Statementl
while (expression) {statement[s]3; statement2}
‘for’ loops tend to be used when one needs to maintain an iterator value. Usually, as in the following example, the first statement initializes the iterator, the condition evaluates it against an end value, and the second statement changes the iterator value.
for (int a =0; a<5; a++)
{
system.console.writeLine(a);
}
foreach loops
syntax:foreach (variablel in variable2) statement[s]
The ‘foreach’ loop is used to iterate through the values contained by any object which implements the IEnumerable interface. When a ‘foreach’ loop runs, the given variablel is set in turn to each value exposed by the object named by variable2. As we have seen previously, such loops can be used to access array values. So, we could loop through the values of an array in the following way:
int[] a = new int[]{1,2,3};
foreach (int b in a)
system.console.writeLine(b);
The main drawback of ‘foreach’ loops is that each value extracted (held in the given example by the variable ‘b’) is read-only
(b) Jump Statements
The jump statements include
• break
• continue
• goto
• return
• throw
break
The following code gives an example – of how it could be used. The output of the loop is the numbers from. 0 to 4.
int a = 0;
while (true)
{
system.console.writeLine(a);
a++;
if (a == 5)
break;
}
Continue
The ‘continue’ statement can be placed in any loop structure. When it executes, it moves the program counter immediately to the next iteration of the loop. The following code example uses the ‘continue’ statement to count the number of values between 1 and 100 inclusive that are not multiples of seven. At the end of the loop the variable y holds the required value.
int y = 0;
for (int x=l; x<101; x++)
{
if ((x % 7) == 0)
continue;
y++;
}
Goto
The ‘goto’ statement is used to make a jump to a particular labeled part of the program code. It is also used in the ‘switch’ statement described below. We can use a’goto’ statement to construct a loop, as in the following example (but again, this usage is not recommended):
int a = 0;
start:
system.console.writeLine(a);
a++;
if (a < 5)
goto start;
(c) Selection Statements
C# offers two basic types of selection statement:
• if-else
• switch-default
if-else
‘if-else’ statements are used to run blocks of code conditionally upon a boolean expression evaluating to true. The ‘else’ clause, present in the following example, is optional.
if (a == 5)
system.console.writeLine(“A is 5”);
else
system.console.writeLine(“A is not 5”);
‘if’ statements can also be emulated by using the conditional operator. The conditional operator returns one of two values, depending upon the value of a boolean expression. To take a simple example,
int i = (myBoo1ean) ? 1 : 0;
The above code sets i to 1 if myBoolean is true, and sets i to 0 if myBoolean is false. The ‘if statement in the previous code example could therefore be written like this:
system.console.writeLine (a==5 ? “A is 5” “A is not 5”);
switch-default
‘switch’ statements provide a clean way of writing multiple if – else statements. In the following example, the variable whose value is in question is ‘a’. If ‘a’ equals 1, then the output is ‘a>0’; if a equals 2, then the output is ‘a> 1 and a>0’. Otherwise, it is reported that the variable is not set.
switch(a)
{
case 2:
Console.writeLine(“a>l and “);
goto case 1;
case 1:
console.writeLine(“a>0”);
break;
default:
console.writeLine(“a is not set”);
break;
}
Each case (where this is taken to include the ‘default’ case) will either have code specifying a conditional action, or no such code. Where a case does have such code, the code must (unless the case is the last one in the switch statement) end with one of the following statements:
break; .
goto case k; (where k is one of the cases specified)
goto default;
From the above it can be seen that C# ‘switch’ statements lack the default ‘fall through’ behavior found in C++ and Java. However, program control does fall through wherever a case fails to specify any action. The following example illustrates this point; the response “a>0”
is given when a is either 1 or 2.
switch(a)
{
case 1:
case 2:
Console.WriteLine(“a>0”);
break;
default:
Console.WriteLine(“a is not set”);
break;
}