我有一些书中的示例代码,作者总是在if结尾处使用continue.
例:
int a = 5;
if(a == 5)
{
// some code
continue;
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这没有任何意义.可能背后有某种质量管理推理还是我错过了一些更重要的观点?
我正在浏览关于continue关键字的问题,以便更好地理解它,我在这个答案中偶然发现了这一行
这些可以是维护时间炸弹,因为"继续"/"中断"与循环之间没有直接联系,它继续/打破除上下文之外;
我有这个for循环:
for(Object obj : myArrayList){
if(myArrayList.contains(someParticularData)){
continue;
}
//do something
}
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是 - continue以我上面所做的方式使用它还是有问题吗?如果是,我可以采用的替代方法是什么?任何形式的指导都会有所帮助.谢谢.
更新:我在这种特定情况下的目标是迭代a Collection(ArrayList在这种情况下),并检查是否包含某些特定数据,如果为真,则跳过该迭代.我被指出这myArrayList.contains(someParticularData)是一次性操作,并且最好在循环外执行该检查,这正是我所寻找的.另外,我了解到如果我可以continue根据某些条件使用if(someConditon),我可以通过使用来避免它if(!someCondition).
我有以下循环,continue当内循环内的检查符合条件时,我想要while循环.我在这里找到了一个解决方案(我在下面的例子中应用了),但它适用于c#.
$continue = false;
while($something) {
foreach($array as $value) {
if($ok) {
$continue = true;
break;
// continue the while loop
}
foreach($value as $val) {
if($ok) {
$continue = true;
break 2;
// continue the while loop
}
}
}
if($continue == true) {
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
continue当内部循环被break淘汰时,PHP是否有自己的主循环方式?
假设我有一个可迭代的:球。我想对该循环中所有非蓝色的球做一些事情。据我所知,我有两个选择:
使用 if: else:
for ball in balls:
if ball.blue:
# can do something with blue ball
else:
# now execute code for all balls that are not blue
Run Code Online (Sandbox Code Playgroud)
使用 if: 继续
for ball in balls:
if ball.blue:
# can do something with blue ball
continue
# now execute code for all balls that are not blue
Run Code Online (Sandbox Code Playgroud)
对我来说,这两种结构所能实现的目标没有区别。是否存在有意的差异?是否存在更快、更具可读性等的情况?
很抱歉,代码段已被删除.我正在分裂这个问题
Douglas Crockfod说通常更好地重构continue循环内部.
为什么在循环中继续被认为是坏的?
这可能非常微不足道,但我无法弄明白.
这有效:
function MyFunction(){
//Do stuff
}
foreach($x as $y){
MyFunction();
if($foo === 'bar'){continue;}
//Do stuff
echo $output . '<br>';
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
function MyFunction(){
//Do stuff
if($foo === 'bar'){continue;}
}
foreach($x as $y){
MyFunction();
//Do stuff
echo $output . '<br>';
}
Run Code Online (Sandbox Code Playgroud)
那只产生1美元的产量然后:
Fatal error: Cannot break/continue 1 level
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
如果我continue在while循环中使用它,为什么它会进入无限循环,但在for循环中工作正常?如果我之后使用它,
循环计数器增量i++会在while循环中被忽略continue,但如果它在for循环中则可以工作.
如果continue忽略后续语句,那么为什么不忽略for循环的第三个语句,它包含计数器增量i++?不是的第三条语句for循环随后到continue也应该被忽略,给出的第三个语句for执行循环后的循环体?
while(i<10) //causes infinite loop
{
...
continue
i++
...
}
for(i=0;i<10;i++) //works fine and exits after 10 iterations
{
...
continue
...
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用jinja2在Flask中进行简单的continue内部操作for-loop
{% for num in range(0,10) %}
{% if num%2 == 0 %}
{% print num %}
{% else %}
{% continue %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误
TemplateSyntaxError: Encountered unknown tag 'continue'. Jinja was looking for the following tags: 'endif'. The innermost block that needs to be closed is 'if'.
这是我遵循的jinja2文档... http://jinja.pocoo.org/docs/templates/#loop-controls
我一直试图找到一种方法,continue我for循环到以前的元素。很难解释。
请注意两个,这是一个示例:-
foo = ["a", "b", "c", "d"]
for bar in foo:
if bar == "c":
foo[foo.index(bar)] = "abc"
continue
print(bar)
Run Code Online (Sandbox Code Playgroud)
执行此操作时,当循环到达时'c',它会看到bar等于'c',它将'c'列表中的continues 替换为下一个元素,并且不会打印bar。我希望'b'在if条件为真时在替换后返回到此循环。因此它将'b'再次打印,就像循环从未达到一样'c'
背景:我正在做一个项目。如果发生任何错误,我必须从上一个元素继续解决该错误。
这是一个流程图,如果可以帮助您:
除了进行替换外,我不想修改现有列表。我尝试搜索每个不同的关键字,但没有找到相似的结果。
如何从当前元素的上一个元素继续循环?