标签: continue

如何在groovy的每个循环中使用"continue"

我是groovy的新手(在java上工作),尝试使用Spock框架编写一些测试用例.我需要使用"每个循环"将以下Java代码段转换为groovy代码段

Java代码段:

List<String> myList = Arrays.asList("Hello", "World!", "How", "Are", "You");
for( String myObj : myList){
    if(myObj==null) {
        continue;   // need to convert this part in groovy using each loop
    }
    System.out.println("My Object is "+ myObj);
}
Run Code Online (Sandbox Code Playgroud)

Groovy Snippet:

def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
    if(myObj==null){
        //here I need to continue
    }
    println("My Object is " + myObj)
}
Run Code Online (Sandbox Code Playgroud)

each groovy for-loop continue spock

24
推荐指数
2
解决办法
2万
查看次数

继续认为有害吗?

开发人员是否应该避免在C#中使用continue或在其他语言中使用其等效来强制循环的下一次迭代?支持或反对的论据是否与关于Goto的论点重叠?

language-agnostic loops goto continue

22
推荐指数
6
解决办法
2869
查看次数

在Javascript中从switch case的内部中断循环

我必须使用什么命令,才能离开for循环,也可以//code inside直接从跳转到//code after

//code before
for(var a in b)
    {
    switch(something)
        {
        case something:
            {
            //code inside
            break;
            }
        }
    }
//code after
Run Code Online (Sandbox Code Playgroud)

javascript for-loop continue switch-statement

21
推荐指数
4
解决办法
2万
查看次数

Python:如何告诉for循环从函数继续?

有时我在for循环中需要以下模式.有时在同一个循环中不止一次:

    try:
        var = 'attempt to do something that may fail on a number of levels'
    except Exception, e:
        log(e)
        continue
Run Code Online (Sandbox Code Playgroud)

现在我没有看到一个很好的方法将它包装在一个函数中,因为它不能return continue:

def attempt(this):
    try:
        return this
    except Exception, e:
        log(e)
        # 1. continue # <-- syntax error: continue not properly in loop or
        # 2. return continue # <-- invalid syntax
        # 3.
        return False # <-- this sort of works, but makes me feel powerless
Run Code Online (Sandbox Code Playgroud)

如果我return False比我能:

    var = attempt('to do …
Run Code Online (Sandbox Code Playgroud)

python continue

18
推荐指数
2
解决办法
1万
查看次数

检查文件是否存在,然后继续在Bash中退出

我有一个脚本,它是发送电子邮件的其他链中的一个脚本.

在脚本开始时,我想检查文件是否存在,只有在存在时才继续,否则只是退出.

这是我的脚本的开始:

if [ ! -f /scripts/alert ];
then
    echo "File not found!" && exit 0
else
        continue
fi
Run Code Online (Sandbox Code Playgroud)

但是我不断收到一条消息说:

line 10: continue: only meaningful in a `for', `while', or `until' loop
Run Code Online (Sandbox Code Playgroud)

有什么指针吗?

bash if-statement continue

18
推荐指数
1
解决办法
8万
查看次数

如果通过,如果继续在python中

我看到有人发布了以下答案来说明如果通过和继续之间的区别.我知道"a"列表是[0,1,2],我只是不知道"if not element"的结果是什么?为什么使用continue时,0不打印,只打印1和2?当a = 0时,"if not element"是"if not 0",它有特殊含义吗?

>>> a = [0, 1, 2]
>>> for element in a:
...     if not element:
...         pass
...     print(element)
... 
0
1
2
>>> for element in a:
...     if not element:
...         continue
...     print(element)
... 
1
2
Run Code Online (Sandbox Code Playgroud)

python if-statement continue

18
推荐指数
6
解决办法
4万
查看次数

Java继续标签已弃用?

我有2个fors,嵌套后我有一些代码,如果条件在嵌套for中为真,我不想执行.如果我使用break代码将执行,所以(正如我在SCJP中学到的)我用于continue label;外部for.这是Java的弃用用法吗?老式的?有人建议使用递归或其他东西,但对我来说这是完全正常,简单,最新和完美的方式.

here:
for (bla bla) {
   for (bla bla) {
      if (whatever) continue here;
   }
// some code I don't want to execute if whatever is true
}
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:
如果我将我的问题改为:你怎么能在多个嵌套的fors之间"导航"?这种方法是"推荐"方式吗?因为这就是它在SCJP Book中所说的.如果不是..这将意味着,Katherine SierraBert Bates是错误的?

编辑2:
为什么continue label;气馁?我想要回答OOP或Java的概念或内部工作,可能会出错.

java continue deprecated

17
推荐指数
3
解决办法
2万
查看次数

Python:在循环中的try-finally语句中使用continue

请问以下代码:

while True:
    try:
        print("waiting for 10 seconds...")
        continue
        print("never show this")
    finally:
        time.sleep(10)
Run Code Online (Sandbox Code Playgroud)

始终打印"等待10秒......"的消息,睡10秒钟,然后再做一次?换句话说,finally即使循环是continue-ed ,子句中的语句也会运行吗?

python continue try-finally

17
推荐指数
2
解决办法
8239
查看次数

调用ShowDialog()后如何继续执行代码

Form.ShowDialog()方法导致代码暂停,直到新调用的窗体关闭.我需要在调用ShowDialog()方法后继续运行代码.我用谷歌搜索并阅读有关使用backgroundworker的内容?但这是我第一次听说过,从未使用过它.

Form2 form2this = new Form2();
form2this.ShowDialog();
MessageBox.Show("Something");
Run Code Online (Sandbox Code Playgroud)

单击按钮后执行此代码,如何仍然调用ShowDialog以防止用户与主窗体交互,但仍然允许主窗体继续其工作?

很抱歉,如果已经讨论过,但我发现的一切似乎都很难执行这么简单的任务.我实际上很惊讶它不包含在SHowDialog方法中.例如ShowDialog().继续会很酷.

c# forms continue

17
推荐指数
2
解决办法
3万
查看次数

C#:嵌套条件vs继续语句

在最近使用ReSharper时,它建议我通过反转if条件和使用continue语句来减少某些地方的嵌套.

嵌套条件:

foreach(....)
{
    if(SomeCondition)
    {
        //do some things

        if(SomeOtherNestedCondition)
        {
            //do some further things
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

继续陈述:

foreach(....)
{
    if(!SomeCondition) continue;

    //do some things

    if(!SomeOtherNestedCondition) continue;

    //do some further things
}
Run Code Online (Sandbox Code Playgroud)

我理解为什么你想减少嵌套的性能和内存问题以及两个片段如何相互等同的逻辑,但是从我的开发背景来看,前面的例子在阅读代码时更容易理解.

您更喜欢哪种方法?为什么?你continue在日常代码中使用嵌套ifs吗?

c# resharper continue nested-loops

16
推荐指数
3
解决办法
4900
查看次数