Today, I saw my buddy asked me a question about goto Bubble sorting, but it is useless to implement it with goto:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace DemoMsg
{
Class Program
{
Static void Main (string [] args)
{
Int [] arr = new int [] {1, 5, 6, 9, 2, 45, 36 };
Foreach (int n in arr)
{
Console. WriteLine (n );
}
Console. WriteLine ("sorted ");
GotoOrderBind (ref arr );
Foreach (int n in arr)
{
Console. WriteLine (n );
}
Console. ReadKey ();
}
Private static void GotoOrderBind (ref int [] arr)
{
Int j = 0;
For (int I = 0; I <arr. Length-1; I ++)
{
J = I + 1;
Id: if (arr [I]> arr [j])
{
Int t = arr [I];
Arr [I] = arr [j];
Arr [j] = t;
Goto id;
}
Else
If (j <arr. Length-1)
{
J ++;
Goto id;
}
}
}
}
}
Goto's explanation on MSDN (C # reference:
The goto statement directly passes program control to the mark statement.
A common usage of goto is to pass the control to a specific switch-case label or the default label in the switch statement.
The goto statement is also used to jump out of a deep nested loop.