Friday, December 7, 2012

Goto Statement

goto statement :

The goto statement transfers the program control directly to a labeled statement. It takes one of the following forms: Syntax
goto identifier;
goto case constant-expression;
goto default;

where: identifier A label. constant-expression A switch-case label.
In the first form, the identifier indicates a label located in the current body, the same lexical scope, or an enclosing scope of the goto statement.
A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.
The goto statement is also useful to get out of deeply nested loops.


The following example demonstrates using goto to break out from nested loops.
// statements_goto.cs
// Nested search loops
using System;
public class GotoTest1 {
public static void Main()
int x = 200, y = 4;
int count = 0;
string[,] myArray = new string[x,y];
// Initialize the array:
for (int i = 0; i < x; i++)
    for (int j = 0; j < y; j++)
     myArray[i,j] = (++count).ToString();
// Read input:
Console.Write("Enter the number to search for: ");

// Input a string:
string myNumber = Console.ReadLine();

// Search:
for (int i = 0; i < x; i++)
 for (int j = 0; j < y; j++)
if (myArray[i,j].Equals(myNumber))
 goto Found;
Console.WriteLine("The number {0} was not found.", myNumber);
goto Finish;

Found: Console.WriteLine("The number {0} is found.", myNumber);

Finish: Console.WriteLine("End of search."); } }



Sample Output

Enter the number to search for: 44  The number 44 is found.  End of search.

If you are searching life partner. your searching end with now offer free matrimonial website which offer free message, free chat, free view contact information. so register here : Free matrimonial website