如何退出for循环中嵌套的if-else语句?(C / C ++)

时间:2018-05-15 18:17:19

标签: c++ for-loop break goto nested-if

我的程序中有一个for循环,它是我程序的主循环,它应该可以实时工作。循环中也有一些if-else语句。我希望如果满足任何if条件,程序将退出if-else语句并重试for循环。

for(;;)
{
    if(condition_one)
    {
        do_one();
    }
    else if(condition_two)
    {
        do_two();
    }
    else 
    {
        do_three();
    }

    rest_of_program();
}

我试着像这样编写上面的代码:

for(;;)
{
    if(condition_one)
    {
        do_one();
        goto exitt; 
    }
    else if(condition_two)
    { 
        do_two();
        goto exitt;
    }
    else 
    {
        do_three(); 
    }
    exitt:
    rest_of_program();
}

但似乎有问题,我的程序不起作用!我做错什么了吗?或者有更好的主意吗?

4 个答案:

答案 0 :(得分:2)

我认为你过分复杂了。问题是这个代码:

M

相当于这一个:

for(;;)
{
    if(condition_one)
    {
        do_one();
        goto exitt; 
    }
    else if(condition_two)
    { 
        do_two();
        goto exitt;
    }
    else 
    {
        do_three(); 
    }
    exitt:
    rest_of_program();
}

for(;;) { if(condition_one) { do_one(); } else if(condition_two) { do_two(); } else { do_three(); } rest_of_program(); } 的各个块是互斥的,无论执行哪一个,之后都会有if-else,然后循环继续。

答案 1 :(得分:1)

使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:gravity="center" android:id="@+id/linearLayoutLeft"> <ImageView android:id="@+id/buildMonsterIcon" android:layout_width="wrap_content" android:layout_height="200dp" android:paddingBottom="5dp" android:paddingRight="15dp" android:paddingTop="15dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buildMonsterName" android:layout_marginBottom="10dp" android:paddingRight="10dp" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/linearLayoutRight"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="?attr/actionBarTabBarStyle" android:id="@+id/buildToolbar"> </android.support.v7.widget.Toolbar> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buildList" android:paddingRight="5dp" /> </LinearLayout> </LinearLayout> 声明。它将退出break循环:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/break

答案 2 :(得分:1)

你不能尝试一个新的IF语句,把它放在for循环的顶部吗?

    'images' => ['uses' => '1.2.3.4'],

我认为这基本上就是你所要求的。检查两者是否都满意然后返回循环,否则继续检查列表。

顺便说一句,更好的方式这样做比使用goto更好,但我还没有看到你的完整代码,所以很难找到一个可行的解决方案。

编辑:正如下面的user463035818所指出的,for(;;) { if(condition_one && condition_two){ continue } else if(condition_one) {do_one();} else if(condition_two) {do_two();} else {do_three();} rest_of_program(); } 将代替goto。起初我有点困惑,但他是对的,它会跳过当前的迭代。

答案 3 :(得分:1)

由于尚未提及,因此还可以选择使用continue语句重置为循环开始,而不会立即退出。请注意,此for循环中执行迭代器语句。

相关问题