Saturday, March 6, 2010

Types of statements in C#

Like any other language, C# also contains three types of statements:
• Simple Statements
• Compound statements
• Control Statements

Simple Statements

A simple statement is any expression that terminates with a semicolon.For example
var= var1+ var2;
i++;

Compound Statements

Related statements can be grouped together in braces to form a compound statement or block.
For example
{
in i=10;
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 the block remain in scope up to the closing brace. Blocking improves the readability of the program code.

Control Statements

Control statements can be furthur divided into three statements:
1. Loop statements
2. Jump statements
3. Selection statements

1.Loop Statements: C# provides a number of loop statements:

while loop

Syntex:

while(expression)
{
statements;
}

eg.

int i = 0;
while ( i < 5 )
{
Console.WriteLine ( i );
i++;
}

do-while loop

Syntex:

do
{
statements'
}
while(Expression);

eg.

class Program
{
static void Main()
{
int[] ids = new int[] { 6, 7, 8, 10 };

// Use do while loop to sum numbers in 4-element array.

int sum = 0;
int i = 0;
do
{
sum += ids[i];
i++;
} while (i < 4);

System.Console.WriteLine(sum);
}
}

Output of the program is:
31

The basic difference between while loop and do-while is the block of statements in do-while loop must be executed atleast once even if the condition is false. In case of while loop, first the condition is checked and if it is true then the block of statements is executed. On the other hand, in case of do-while loop first the block of statements is executed and then the condition is checked.And now if the condition is true then again the block of statements is executed.

for loop

Syntex: for(Initialization;Finalization;Increment/Decrement)
{
statements;
}

eg.

for(int i=0;i<=6;i++)
{
Console.WriteLine(i);
}

for loops tend to be used when one needs to maintain an iterator value. Usually, as in the above example, the first statement initializes the iterator, the second condition evaluates it against as end value, and the next statement changes the iterator value.

foreach loop

Syntex: foreach( variable1 in variable2)
{
statements;
}

eg.

int [] a=new int []{1,2,3};
foreach(int b in a)
Console.WriteLine(b);

2.Jump Statements: The jump statements include:

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)
{
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 multiple of seven.

int b=0;
for(int a=1;a<101;a++)
{
if(a%7==0)
continue;
b++;
}

goto loop

The goto statement is used to make a jump to a particular labeled part of the program code.We can use goto statement to construct a loop as in the following example(but this usage is not recommended.

int a=0;
start:
Console.WriteLine(a);
a++;
if(a<5)
goto start;


3.Selection Statements: C# offers two basic type of selection statements:

if-else loop

'if-else' statement 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)
Console.WriteLine("A is 5");
else
Console.WriteLine(" A is not 5");

switch

'switch' statement provide a clean way of writing multiple if-else statements. Switch statement uses cases to check against the condition.There is use of break statement after every case.

Syntex:
switch(Integer Value)
{
case 1:
Statements;
break;

case 2:
Statements;
break;

default:
Statements;
break;
}

eg.

// statements_switch.cs
using System;
class SwitchTest
{
public static void Main()
{
Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
Console.Write("Please enter your selection: ");
string s = Console.ReadLine();
int n = int.Parse(s);
int cost = 0;
switch(n)
{
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
case 3:
cost += 50;
goto case 1;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
if (cost != 0)
Console.WriteLine("Please insert {0} cents.", cost);
Console.WriteLine("Thank you for your business.");
}
}

For next tutorial click the following link

http://dotnettutions3.blogspot.com