简体   繁体   English

这将继续导致嵌套的for循环做什么?

[英]What will this continue cause the nested for-loop to do?

I've searched SO and can find questions addressing similar subjects, but nothing for this specific question and language. 我已经搜索了SO,可以找到针对相似主题的问题,但是对于此特定问题和语言却一无所获。 After reading some Q&A and not finding the answer, I searched , and and got zero results. 在阅读了一些问答之后,并没有找到答案,我搜索了 ,结果为零。

I've been asked this question in a university quiz: 在大学测验中有人问我这个问题:

If I have: 如果我有:

int n = 3;
   for (int i = 1; i <= n; i++)  {
      System.out.print(" x ");
        for (int j = 1; j <= n; j++)  {
         System.out.println(" x ");
           continue;
           //no content here
        }
     }

Without there being any content after the continue statement; 在continue语句之后没有任何内容; how does this using continue affect this loop? 使用continue如何影响此循环? Does it cause a break in the second loop of does the loop continue to iterate? 它会导致第二个循环中的中断吗?该循环是否会继续迭代?

A continue statement without a label will re-execute from the condition the innermost while or do, or loop, and from the update expression the innermost for loop . 没有标签的continue语句将从最里面的while或do或者loop循环中的条件重新执行,并从更新表达式的最里面的for loop重新执行 It is often used to early-terminate a loop's processing and thereby avoid deeply-nested if statements. 它通常用于尽早终止循环的处理,从而避免深层嵌套if语句。

Hence for your program , the keywork continue doesn't make much sense. 因此,对于您的程序而言, continue进行按键并没有多大意义。 It is used as a kind of a escape thing. 它被用作一种逃生的东西。 For eg: 例如:

aLoopName: for (;;) {
  // ...
  while (someCondition)
  // ...
    if (otherCondition)
      continue aLoopName;

Say, if you modify your program like: 说,如果您修改程序,例如:

int n = 3;
   for (int i = 1; i <= n; i++)  {
      System.out.print(" x ");
        for (int j = 1; j <= n; j++)  {
         if(j%2!=0)
         {
          System.out.println(" x ");
          continue;
         }
         else
         break;
     }

This will break the inner for loop for j=2. 对于j = 2,这将破坏内部的 for循环。 Hope you understand. 希望你能理解。 :) :)

The answer to your question: 您问题的答案:

How does this using continue affect this loop? 使用continue如何影响此循环? Does it cause a break in the second loop of does the loop continue to iterate? 它会导致第二个循环中的中断吗?该循环是否会继续迭代?

is: 是:

The second loop will not break, it will continue to iterate. 第二个循环不会中断,它将继续进行迭代。 break keyword is for breaking loop. break关键字用于中断循环。

EDIT 编辑

Suppose you have for loop: 假设您有for循环:

for(int i = 0; i < 5; i++){
   continue;
}

continue in for executes the statement of for loop ( i++ ) to continue to the next iteration. continue in for执行for循环( i++ )语句以继续进行下一个迭代。

In other loops, while{} or do{}while(); 在其他循环中, while{}do{}while(); the things will not be like this and may cause infinite loops. 事情不会像这样,并可能导致无限循环。

If there was a code under continue , it'll be a dead code (unreachable code). 如果在continue下有一个代码,它将是无效代码 (无法访问的代码)。

The way you wrote it, it has no effect since it's already the last line. 您编写它的方式没有效果,因为它已经是最后一行了。 The loop would have continued if there was no continue; 如果没有continue;循环将continue; .

These two blocks of code have the same effect: 这两个代码块具有相同的效果:

for(int i = 0; i < 10; i++) {
   // .. code ..
}

for(int i = 0; i < 10; i++) {
   // .. code ..
   continue;
}

However, the below snippet has unreachable code: 但是,以下代码段具有无法访问的代码:

for(int i = 0; i < 10; i++) {
   // .. code ..
   continue;
   // .. unreachable code ..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM