什么是一次许多突破for循环的一种巧妙的方法? [英] What is a neat way of breaking out of many for loops at once?

查看:108
本文介绍了什么是一次许多突破for循环的一种巧妙的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我需要在最内层的循环里面的一些事件的发生立即打出来三四个嵌套的for循环。 什么是这样做的一个非常简洁的方式?

Suppose I need to break out of three or four nested for loops at once at the occurence of some event inside the innermost loop. What is a neat way of doing that?

我要做的就是使用标记是这样的:

what I do is use flags like this:

int i, j, k;
int flag1 = 0;
int flag2 = 0;

for (i = 0; i < 100; i++) {
    for (j = 0; j < 100; j++) {
        for (k = 0; k < 100; k++) {
            if (k == 50) {
                flag1 = 1;
                flag2 = 1;
                break;
            }
        }
        if (flag1 == 1)break;
    }
    if (flag2 == 1)break;
}

我不认为这是特别整齐。

I don't think this is particularly neat.

你将如何完成同样的事情?(W / O使用跳跃)

How would you accomplish the same thing? (w/o using jumps)

推荐答案

使用goto语句。它的清洁,简单。

use goto. it's clean and simple.

这篇关于什么是一次许多突破for循环的一种巧妙的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆