正常计算机代码的执行顺序是从上至下,除非有逻辑语句跳转。
以前没了解goto的使用,现在研究一下goto还是挺有意思的。
下面是c++简单的代码demo。
void Main::xecutionTest()
{
for (int i = 0; i<10; i++)
{
switch (getCode())
{
case 0:
cout << "Ignore this item";
continue;
case 1:
mCase:
cout << "End the whole cycle";
break;
case 2:
cout << "Jump to the specified position";
goto mCase;
case 3:
cout << "End this function";
return;
default:
cout <<"Invalid selection";
break;
}
}
cout << "Normal execution";
}
下面是c#简单的代码(c++也适用c#)demo。
void xecutionTest()
{
for (int i = 0; i < 10; i++)
{
switch (getCode())
{
case 0:
Console.WriteLine("Ignore this item");
continue;
case 1:
Console.WriteLine("End the whole cycle");
break;
case 2:
Console.WriteLine("Jump to the specified position");
goto case 1;
case 3:
Console.WriteLine("End this function");
return;
default:
Console.WriteLine("Invalid selection");
break;
}
}
Console.WriteLine("Normal execution");
}
下面是java简单的代码demo 。
public static void main(String[] args) {
for (int i = 0; i<10; i++)
{
switch (getCode())
{
case 0:
System.out.println("Ignore this item");
continue;
case 1:
mCase:
System.out.println("End the whole cycle");
break;
case 2:
System.out.println("Jump to the specified position");
break mCase;
case 3:
System.out.println("End this function");
return;
default:
System.out.println("Invalid selection");
break;
}
}
System.out.println("Normal execution");
}
下面是php简单的代码demo 。
function xecutionTest(){
for (int i = 0; i<10; i++)
{
switch (getCode())
{
case 0:
echo "Ignore this item";
continue;
case 1:
mCase:
echo "End the whole cycle";
break;
case 2:
echo "Jump to the specified position";
goto mCase;
case 3:
echo "End this function";
return;
default:
echo "Invalid selection";
break;
}
}
echo "Normal execution";
}
}