1

我有一个while循环,在里面while loop我有一个foreach loop.

在这里,我学习了如何使用Continue; Return; Break. while loop但是当我在里面的时候我需要离开foreach loop这可能吗?

我在 内部进行交互foreach loop,我想离开 foreach 并进入while. 我该怎么做?像这样:

while (!reader.EndOfStream)  //COMEÇO DO WHILE
{
  ///Some Codes

  foreach(string s in checkedlistbox1.items)
        {
          switch(s)
           {
             case "1":
                if( 1 > 0)
                 {
                    ///HERE I WANT TO GO TO THE NEXT INTERACTION OF THE WHILE
                    ///When I use CONTINUE; HERE, I GO TO THE NEXT INTERACTION OF THE FOREACH. BUT I NEED TO GO TO THE NEXT OF THE WHILE.
                 }
           }
        }
}

这就是我想要做的:我正在逐行读取 file.txt,并用这些值和其他一些东西写一个新的......如果 a为空required,其中一些值可能是(由用户设置)required field,所以我什么都不做,然后去下一个while interaction......

4

8 回答 8

4

您需要从开关断开,然后从 foreach 断开,同时设置变量。

然后,您可以检查该变量以查看是否应该continue进行下while一次迭代。

像这样做:

while (!reader.EndOfStream)
{
    // Some Codes

    bool skipToNext = false;

    foreach (string s in checkedlistbox1.items)
    {
        switch (s)
        {
            case "1":
                if (1 > 0)
                {
                    skipToNext = true;
                    break;
                }
        }

        if (skipToNext) break;
    }

    // in the case of there being more code, you can now use continue
    if (skipToNext) continue;

    // more code
}

此流程工作的示例

var list = new List<string> { "0", "1", "2" };
int a = 0, b = 2;

while (a++ < b)
{
    // Some Codes

    bool skipToNext = false;

    foreach (string s in list)
    {
        Debug.WriteLine("{0} - {1}", a, s);

        switch (s)
        {
            case "1":
                if (1 > 0)
                {
                    Debug.WriteLine("Skipping switch...");
                    skipToNext = true;
                    break;
                }
        }

        if (skipToNext)
        {
            Debug.WriteLine("Skipping foreach...");
            break;
        }
    }

    // in the case of there being more code, you can now use continue
    if (skipToNext)
    {
        Debug.WriteLine("Skipping to next while...");
        continue;
    }

    // more code
}

输出:

1 - 0
1 - 1
Skipping switch...
Skipping foreach...
Skipping to next while...
2 - 0
2 - 1
Skipping switch...
Skipping foreach...
Skipping to next while...
于 2013-02-19T13:14:33.580 回答
1

1)是的,这是可能的。有用。

 foreach() {
    while() {
       break; //leave the while
    }
    //... and continues from here
 }

2)每个while都将在下一次foreach迭代之前结束,所以第二个问题没有多大意义......除非你的意思是在foreach内开始下一个while,在这种情况下......是的!

 foreach() {
    while() {
       ...
    }
    break; //will go to the next foreach iteration, i.e. starts a new while
 }

至于您的代码示例,您在评论中提到的点的中断将满足您的需要(退出 foreach,自然进入下一个 while 迭代)。

编辑:在您发布示例之后,您的问题似乎不在于 while 和 foreach 之间的交互,而在于 switch:break 用作“转到循环中的下一个迭代”和“完成这个”的关键字外壳和开关”。

编译器会将中断视为“开关中断”。您需要自己模仿行为:

    foreach(string s in checkedlistbox1.items)
    {
      bool dobreak = false;
      switch(s)
       {
         case "1":
            if( 1 > 0)
             {
                dobreak = true;
             }
             break; // exit the case
       }
       if (dobreak)
          break; // exits the for (it is a 'foreach-break')
    }
于 2013-02-19T13:10:18.323 回答
1
bool dobreak = false;
while (!reader.EndOfStream && !dobreak )  //COMEÇO DO WHILE
{
  ///Some Codes

  foreach(string s in checkedlistbox1.items)
        {
          switch(s)
           {
             case "1":
                if( 1 > 0)
                 {
                    dobreak = true;
                    break;
                 }
           }
        }
}
于 2013-02-19T13:12:25.987 回答
0

未经测试,但这些是应该回答您的问题的一些可能的方法:

bool continueLooping = true;

string[] array = { "1", "2" };

while (continueLooping)
{
    foreach (string x in array)
    {
        // break out of foreach loop AND while loop
        if (x == "1")
        {
            continueLooping = false;
            break;
        }

        // go to next iteration of foreach loop
        if (x == "2")
        {
            continue;
        }

        // break out of foreach loop and continue while loop
        if (x == "3")
        {
            break;
        }
    }
}
于 2013-02-19T13:14:47.253 回答
0

你需要打破你的foreachwhile这样的事情:

bool abort = false;
while (!reader.EndOfStream && !abort)  //COMEÇO DO WHILE
{
  ///Some Codes

  foreach(string s in checkedlistbox1.items)
        {
          switch(s)
           {
             case "1":
                if( 1 > 0)
                 {
                    ///HERE I WANT TO LEAVE TO THE NEXT INTERACTION OF THE WHILE
                    abort = true;
                    break;
                 }
           }

           if (abort)
           {
               break;
           }
    }
}
于 2013-02-19T13:16:14.203 回答
0

我是 C# 的初学者,但我建议您在 while 循环中添加另一个条件,您可以自己设置。然后,在需要时更改它。例如:

MyKeyCondition = 0;

while(MainCondition||MyKeyCondition){

if(MyCheckCondition){
MyKeyCondition = 1;
}

}

现在,如果您更改关键条件,即使不满足主要条件,您也可以处理 while 循环。

于 2013-02-19T13:16:35.573 回答
0

可以试试goto;

while (true)
{
 foreach(string s in checkedlistbox1.items)
  {
   switch(s)
   {
     case "1":
        if( 1 > 0)
        {
          goto WhileOut;
        }
    }
  }
}
WhileOut:
      // some code
于 2013-02-19T13:18:28.600 回答
0

通常嵌套循环表明方法应该被拆分。

在这个例子中,它可以使代码更清晰。如果将内部for循环放入单独的方法中,则可以使该方法返回一个bool. true如果希望外while循环继续迭代,则返回;否则,false如果您希望它退出while循环,则返回。

它看起来像这样:

private void doSomething(StreamReader reader)
{
    while (!reader.EndOfStream)
    {
        // Some Codes...

        if (!processNextItem(reader))
        {
            break;
        }
    }
}

// Returns true if the caller should continue its while loop; false if it should exit it.

private bool processNextItem(StreamReader reader)
{
    foreach (string s in checkedlistbox1.items)
    {
        switch (s)
        {
            case "1":
                if (1 > 0)
                {
                    return false; // Return false to exit while loop.
                }
        }
    }

    return true; // Return true to continue while loop.
}

我不确定你需要传递给什么processNextItem();我刚刚通过reader了一个例子。

于 2013-02-19T13:30:48.037 回答