Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript 标记语句 #19

Open
RicoLiu opened this issue Jul 1, 2019 · 0 comments
Open

JavaScript 标记语句 #19

RicoLiu opened this issue Jul 1, 2019 · 0 comments

Comments

@RicoLiu
Copy link
Owner

RicoLiu commented Jul 1, 2019

在进行双重 for 循环计算时,我们希望满足条件跳出循环,以免不必要的资源浪费。

此时,我们就可以使用 labelcontinue/break 配合使用。

firstLoop: 
for (let i = 0; i < 3; i++) { 
   for (let j = 0; j < 3; j++) {
      if (i === j) {
         continue firstLoop; // 继续 firstLoop 循环
         // break firstLoop; // 中止 firstLoop 循环
      }
      console.log(`i = ${i}, j = ${j}`);
   }
}
// 输出
i = 1, j = 0
i = 2, j = 0
i = 2, j = 1
 
for (let i = 0; i < 3; i++) { 
   for (let j = 0; j < 3; j++) {
      if (i === j) {
         continue 
      }
      console.log(`i = ${i}, j = ${j}`);
   }
}
// 输出
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 2
i = 2, j = 0
i = 2, j = 1

参考:

https://juejin.im/post/5d1716e4f265da1bbc6fea1e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant