Monday, December 15, 2008

break Satement:

Definition:

This statement is often used in conjunction with switch statements; however, C# allows you to use it to break out of the current block of statements. The break statement is usually used to break out from an iteration statement block.

Example:


static void Main(string[] args)

{

int i = 0;

while (i !=5)

{

Console.WriteLine(i);

Console.ReadLine();

if (i == 2)

break;

i++;

}

}


Output:

0
1
2

continue Statement:

Definition:

This statement returns the control back to the Boolean expression that controls an iteration statement.

Example:


static void Main(string[] args)

{

for (int i = 1; i <= 4; i++)

{

if (i !=1)

{

Console.WriteLine(i);
Console.ReadLine();

continue;

}

}

}

Output:

2
3
4

goto Statement:

Definition:

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.

This statement transfers control to a labeled statement unconditionally. In C#, any statement can be labeled. A colon follows a statement label, and a label identifier follows the goto keyword. The goto statement transfers control to the statement named by the label identifier.

If you overuse the goto statement, it can create code that is hard to read and follow. In general, it's a good practice to avoid using goto altogether by restructuring the code in a way that allows you to get around using the goto statements.

Example:

static void Main(string[] args)

{

goto A;

B: Console.WriteLine("Hello Hems");

Console.ReadLine();

return;

A: Console.WriteLine("Hello Dulip");

Console.ReadLine();

goto B;

}

Output:

Hello Dulip;
Hello Hems;

Sunday, December 14, 2008

do-while loops

Definition:

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 '2':

Example:

class Program

{

static void Main(string[] args)

{

int a=2;

do

{

Console.WriteLine(a);

Console.ReadLine();

a++;

}

while (a > 3);

}

}


Foreach Statement

Definition:

syntax: foreach (variable1 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 variable1 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:

Example:1

class Program

{

static void Main(string[] args)

{

String[] List={"Hemnath","Mani","Siva","Arivu","Jagan",

"Nagendra","John","Anand"};

foreach (string Names in List)

{

Console.WriteLine("{0}", Names);

Console.ReadLine();

}

}

}


Example:2

private void Form1_Load(object sender, EventArgs e)

{

string conn = "Data Source=localhost;Initial Catalog=master;Integrated Security=True";

SqlConnection connection = new SqlConnection(conn);

SqlDataAdapter da = new SqlDataAdapter("select * from Student", connection);

DataSet ds = new DataSet();

DataTable dt = new DataTable("Student");

da.Fill(ds);

foreach (DataRow dr in ds.Tables[0].Rows)

{

ListViewItem itm = new ListViewItem();

itm.Text = Convert.ToString(dr["Name"]);

itm.SubItems.Add(Convert.ToString(dr["Name"]));

listBox1.Items.Add(itm);

}

}

Note:
The main drawback of 'foreach' loops is that each value extracted (held in the given example by the variable 'List') is read-only.