Custom Search

Tuesday, April 1, 2008

The continue and goto statements.

The continue statement basically what is does is that it return controls to the boolean expressing that controls an iteration statement. Example:

for (int i = -6; i != 0; i++)

if (i == -3)


Console.WriteLine("i value: " + i);

So when the continue statement is executed under the if condition, it tells the for iteration to continue to the next iteration, and because of this the value -3 does not appear in the console.

The Continue statement is commonly used in while, do, for, switch and foreach statements.

The goto statement is very useful when you need to unconditionally transfer control to a labelled statements. In C# to make a labels statement is very easy, just place a name for the labelled followed by “:” on any statement. Example:

for (int j = 0; j != 7; j++)

Console.WriteLine("j value: " + j);
if (j == 4)

goto Exit;
Exit: Console.WriteLine("Exit reached, loop finished");

You can also use it in a switch statement by writing for example, “goto case 2”.

No comments: