如何继续,跳转语句影响c#中的循环

时间:2018-01-24 19:10:33

标签: c#

我正在努力阅读C-Sharp 7.0 - Joseph Albahari, 在跳转语句部分,有一个像

这样的代码
{
  "name": "DotnetNewAngular3",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js",
    "postinstall": "webpack --config webpack.config.vendor.js"
  },
  "devDependencies": {
    "@angular/animations": "^4.4.6",
    "@angular/common": "^4.4.6",
    "@angular/compiler": "^4.4.6",
    "@angular/compiler-cli": "^4.4.6",
    "@angular/core": "^4.4.6",
    "@angular/forms": "^4.4.6",
    "@angular/http": "^4.4.6",
    "@angular/platform-browser": "^4.4.6",
    "@angular/platform-browser-dynamic": "^4.4.6",
    "@angular/platform-server": "^4.4.6",
    "@angular/router": "^4.4.6",
    "@ngtools/webpack": "1.5.0",
    "@types/chai": "4.0.1",
    "@types/jasmine": "2.5.53",
    "@types/webpack-env": "1.13.0",
    "angular2-router-loader": "0.3.5",
    "angular2-template-loader": "0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "3.2.1",
    "bootstrap": "3.3.7",
    "chai": "4.0.2",
    "css": "2.2.1",
    "css-loader": "0.28.4",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.9",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "2.1.2",
    "file-loader": "0.11.2",
    "html-loader": "0.4.5",
    "isomorphic-fetch": "2.2.1",
    "jasmine-core": "2.6.4",
    "jquery": "3.2.1",
    "json-loader": "0.5.4",
    "karma": "1.7.0",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-webpack": "2.0.3",
    "preboot": "4.5.2",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "5.5.6",
    "style-loader": "0.18.2",
    "to-string-loader": "1.1.5",
    "typescript": "2.4.1",
    "url-loader": "0.5.9",
    "webpack": "2.5.1",
    "webpack-hot-middleware": "2.18.2",
    "webpack-merge": "4.1.0",
    "zone.js": "0.8.12"
  }
}

输出为:1 3 5 7 9

但是当我评论下面的继续跳转声明时

for (int i = 0; i < 10; i++)
{
if ((i % 2) == 0) // If i is even,
continue; // continue with next iteration
Console.Write (i + " ");
}

输出为:0 2 4 6 8

有人可以解释一下continue语句如何影响循环流程吗?

4 个答案:

答案 0 :(得分:2)

如果没有大括号,当if的条件为真时,C#将编译要执行的下一个语句。评论不是声明。这是最好总是包括大括号的主要原因之一,即使只有一个语句。

continue告诉程序跳转到循环的开始并重新测试条件。在这种情况下,将跳过Console.Write次来电。

还有break完全结束循环并且不会重新测试条件。

答案 1 :(得分:2)

我认为你的根本混淆与注释掉一个语句的事实有关,删除语句完全。它没有做什么&#34;什么都不做&#34;声明,然后是if

的正文

但是,我认为对于continue所做的事情也有一种混淆。理解这一点的一个好方法是将其简化为更简单的东西。

假设你有

for (int i = 0; i < 10; i++)
{
  if ((i % 2) == 0)
    continue;
  Console.Write (i + " ");
}

for复杂且while不那么复杂的意义上,让我们将其简化为更简单的程序。您的程序片段与:

相同
{
  int i = 0;
  while (i < 10)
  {
    if ((i % 2) == 0)
      goto DoTheLoopIncrement;
    Console.Write (i + " ");
    DoTheLoopIncrement: 
    ++i;
  }
}

与以下内容相同:

{
  int i = 0;
  DoTheLoopTest:
  if (i < 10)
    goto DoTheLoopBody;
  else
    goto DoneTheLoop;
  DoTheLoopBody:
  {
    if ((i % 2) == 0)
      goto DoTheLoopIncrement;
    Console.Write (i + " ");
    DoTheLoopIncrement: 
    ++i;
    goto DoTheLoopTest;
  }
}
DoneTheLoop:
...

请注意,阅读&#34; goto&#34;需要多长时间?版本是。 这就是我们使用whilefor的原因。必须明白这正是while和{ {1}}和for正在做的是为了理解他们的控制流程。它们只是写continue的一种愉快方式。

现在:你明白goto是什么意思吗?休息只是写break的一种愉快方式。

答案 2 :(得分:1)

格式不佳:

for (int i = 0; i < 10; i++)
{
if ((i % 2) == 0) // If i is even,
continue; // continue with next iteration
Console.Write (i + " ");
}

更好的格式化:

// Output is : 1 3 5 7 9 
for (int i = 0; i < 10; i++)
{
   if ((i % 2) == 0) // If i is even,
      continue; // continue with next iteration
   Console.Write (i + " ");
}

没有&#34;继续&#34;:

// Output is : 0 2 4 6 8
for (int i = 0; i < 10; i++)
{
   if ((i % 2) == 0) // If i is even,
     //   continue; // continue with next iteration
     Console.Write (i + " ");
}

最佳格式:

// Output is : 1 3 5 7 9 
for (int i = 0; i < 10; i++)
{
   // If i is even,
   if ((i % 2) == 0) 
   {
      // continue with next iteration
      continue; 
   }
   Console.Write (i + " ");
}

答案 3 :(得分:0)

Continue将忽略循环内的所有语句并继续下一次迭代。