我怎么能从循环中出去

时间:2012-12-03 19:08:52

标签: c# while-loop

while (true)
            {
                    while (true)
                    {
                        int h = ok_group_join(gr);
                        if (h == 0)
                            ok_group_post(gr);
                        else if (h == -1)
                        {
                          //How can I go to first while cycle?
                        }

我在我的代码中写了一条评论,我怎样才能从那里回到第一次循环?

3 个答案:

答案 0 :(得分:1)

while (true)
            {
                string acc = "";
                lock (accslocker)
                {
                    if (accs.Count == 0)
                    {
                        break;
                    }
                    else
                        acc = accs.Dequeue();
                }
                string cook = od_auth(acc);
                if (cook != "badacc")
                {
                    string gr;
                    while (true)
                    {
                        int h = ok_group_join(gr);
                        if (h == 0)
                            ok_group_post(gr);
                        else if (h == -1)
                        {
                          //How can I go to first while cycle?
                          break;
                        }

break关键字会让您退出。

答案 1 :(得分:1)

使用break

else if (h == -1)
{
    break;
}

这会让你摆脱当前的循环,导致你回到上一个while循环。

答案 2 :(得分:0)

甚至比使用break更好的是将内循环重写为while (true)

while (true)
{
    int h = 0;
    while (h != -1)
    {
        h = ok_group_join(gr);
        if (h == 0)
            ok_group_post(gr);
    }
 }