gpt4 book ai didi

Julia - 继续外循环

转载 作者:行者123 更新时间:2023-12-05 00:17:20 26 4
gpt4 key购买 nike

我目前正在将一个算法从 Java 移植到 Julia,现在我遇到了一个部分,当满足某些条件时,我必须从内部循环继续外部循环:

 loopC: for(int x : Y){
for(int i: I){
if(some_condition(i)){
continue loopC;
}
}
}

我在 GitHub 上发现了一些关于这个主题的问题,但似乎只有关于它的讨论,还没有解决方案。有人知道如何在 Julia 中实现这一点吗?

最佳答案

就像在其他一些语言中 julia 使用的一样 break为了这:

for i in 1:4
for j in 1:4
if j == 2
break
end
end
end

当 j 为 2 时跳出内循环

但是,如果您需要退出外循环,您可以像这样使用@goto 和@label
for i in 1:4
for j in 1:4
if (j-i) == 2
@goto label1
end

if j == 2
@goto label2
end
do stuff
end
@label label2
end
@label label1

直接来自 julia 文档 http://docs.julialang.org/en/release-0.5/manual/control-flow/

It is sometimes convenient to terminate the repetition of a while before the test condition is falsified or stop iterating in a for loop before the end of the iterable object is reached. This can be accomplished with the break keyword

关于Julia - 继续外循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com