Switch语句,中断for循环以返回到Switch
我在主while循环中有一个开关,可以运行我的游戏。我正试图打破我的开关,以便转到另一个案件。下面的例子更好地解释了这一点:Switch语句,中断for循环以返回到Switch,c,C,我在主while循环中有一个开关,可以运行我的游戏。我正试图打破我的开关,以便转到另一个案件。下面的例子更好地解释了这一点: int j = 0; While(1){ switch(j){ case 0: .... break; case 1: for( i =0; i > 100; i++){ if(lives == 0) j = 2; break; //this is where I want to brea
int j = 0;
While(1){
switch(j){
case 0: ....
break;
case 1:
for( i =0; i > 100; i++){
if(lives == 0)
j = 2;
break; //this is where I want to break out of my switch to go to case 2. But it
//breaks out of my for loop. I do not know how to get around this. Thank
//you!
}
case 2: //some stuff I want to do
}
}
如果您想突破for并直接进入案例2,那么您的代码应该完全做到这一点。您不能突破多个可突破的作用域,只能突破最高的作用域 对于这一点,草率的“解决方案”是使用跳转 真正的解决方案是将函数分解成更小的逻辑小函数,然后可以随意分解或返回。出于代码质量和清洁的目的,for循环几乎肯定应该在它自己的函数中
bool doSomethingInALoopWithADescriptiveName(int &var)
{
for(int i = 0; i < 100; i++)
{
if(lives_WhichShouldntBeGlobal == 0)
{
var = 2;
return false;
}
}
return true;
}
void myFuncWithADescriptiveName()
{
int variableWithADescriptiveName = 0;
while(true)
{
switch(j)
{
case 0:
{
//....
} break;
case 1:
{
bool continueLooping = doSomethingInALoopWithADescriptiveName(&variableWithADescriptiveName);
if(!continueLooping)
{
return;
}
} break;
case 2:
{
//some stuff I want to do
} break;
}
}
}
bool dosomethinginaloop,带有描述性名称(int&var)
{
对于(int i=0;i<100;i++)
{
if(lives_WhichShouldntBeGlobal==0)
{
var=2;
返回false;
}
}
返回true;
}
void myFuncWithADescriptiveName()
{
int variableWithADescriptiveName=0;
while(true)
{
开关(j)
{
案例0:
{
//....
}中断;
案例1:
{
bool continueLooping=doSomethingInALoopWithADescriptiveName(&variableWithADescriptiveName);
如果(!continueLooping)
{
返回;
}
}中断;
案例2:
{
//一些我想做的事情
}中断;
}
}
}
注意:由于问题的标记问题,我不确定问题所问的是哪种语言。我的答案适用于多种语言(可以总结为“编写简单、紧凑、自描述性且干净的代码”),但我的代码示例是伪C++语言,但可以轻松应用于其他语言。将大括号放在触发下一种情况的条件周围
switch(j){
case 0: ....
break test;
case 1:
for( i =0; i > 100; i++){
if(lives == 0)
j = 2;
break; //this is where I want to break out of my switch to go to case 2. But it
//breaks out of my for loop. I do not know how to get around this. Thank
//you!
}
break test;
case 2: //some stuff I want to do
break test;
}
test://After switch.
case 1:
for( i =0; i > 100; i++)
{
if(lives == 0)
{
j = 2;
break; // This exits the for effectively jumping at the case break
}
// I suppose here you have other code to process
// that should not be executed if (lives == 0)
}
break;
您的中断正在打破for循环。因此,您需要添加一些额外的逻辑来检查循环是否提前退出,然后再次调用break 其次,for(i=0;i>100;i++)将永远不会执行,因为我永远不会>100
for(int i = 0; i < 100; i++)
{
if(lives == 0)
{
j = 2;
quit = true;
break; //Break out of the loop
}
}
//Note, if this language is C, control will just fall through to case 2 anyway.
//If C#, you have to add a break at the end of this case regardless
if(quit == true)
{
break; //Break out of switch
}
for(int i=0;i<100;i++)
{
如果(寿命==0)
{
j=2;
退出=真;
break;//中断循环
}
}
//注意,如果这种语言是C语言,那么无论如何,控制权都将落入案例2中。
//如果是C#,则无论如何都必须在本例末尾添加一个中断
if(quit==true)
{
break;//开关断开
}
我想你只想在生存==0时点击案例2
。如果是这样,你需要把你的中断在{}
中的code>和j=2代码>-这只会在生存==0
时中断循环。然后,如果您放置一个断点代码>在案例0的结尾处,您将终止开关,然后由于而重新输入开关,同时(1)
,然后点击案例2:
您的for循环应该正确地设置为(i=0;i<100;i++)的for(i=0;i>100;i++)
intj=0;
而(1){
开关(j){
案例0:。。。。
打破
案例1:
对于(i=0;i<100;i++){
如果(寿命==0){
j=2;
打破
}
}
打破
案例2://一些我想做的事情
}
}
好的,您的循环实际上在正确的轨道上,但是,正如前面提到的,您的循环状况需要检查
int j = 0;
While(1){
switch(j){
case 0: ....
break;
case 1:
for( i =0; i < 100; i++){
if(lives == 0){
j = 2;
break; // Break for
}
}
if(j != 2) break; // If no break in the switch's case, then continue to the next case
case 2: //some stuff I want to do
break;
} //End Switch
} //End While
intj=0;
而(1){
开关(j){
案例0:。。。。
打破
案例1:
对于(i=0;i<100;i++){
如果(寿命==0){
j=2;
中断;//中断
}
}
如果(j!=2)中断;//如果开关的情况下没有中断,则继续下一种情况
案例2://一些我想做的事情
打破
}//结束开关
}//结束时
像这样构建一个状态机:
C#
bool OnceMore=true
string ProcessState = "Start"
while(OnceMore)
{
OnceMore = False;
switch(ProcessState)
{
case "Start" :
{
// do something
ProcessState = "Check";
OnceMore = True;
break;
}
case "Check" :
{
// do check
if( Check == "Ok")
{
ProcessState = "Close";
}
else // try again
{
ProcessState = "Start";
}
OnceMore = True;
break;
}
case "Close" :
{
// do what you need to return to the caller
break;
}
}
}
return
为什么这个标签上有5种不同的语言?这与他们中的大多数人无关。
string ProcessState = "Start"
while(OnceMore)
{
OnceMore = False;
switch(ProcessState)
{
case "Start" :
{
// do something
ProcessState = "Check";
OnceMore = True;
break;
}
case "Check" :
{
// do check
if( Check == "Ok")
{
ProcessState = "Close";
}
else // try again
{
ProcessState = "Start";
}
OnceMore = True;
break;
}
case "Close" :
{
// do what you need to return to the caller
break;
}
}
}
return