如何在foreach循环中保留while循环
本文关键字:循环 while 保留 foreach | 更新日期: 2023-09-27 18:00:11
我有一个while
循环,在这个while loop
内部有一个foreach loop
。
在这里,我学习了如何使用Continue; Return; Break
跳过循环中当前的交互。但当我在foreach loop
内部时,我需要离开while 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,并用这些值和其他一些东西写一个新的。。。其中一些值可能是required
(由用户设置),如果required field
为空,那么我什么也不做,然后转到下一个while interaction
。。。
您需要先断开开关,然后断开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...
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;
}
}
}
}
1)是的,这是可能的。它有效。
foreach() {
while() {
break; //leave the while
}
//... and continues from here
}
2) 每次都会在下一次foreach迭代之前结束,所以第二个问题没有多大意义。。。除非你的意思是在前臂内侧开始下一次,在这种情况下。。对
foreach() {
while() {
...
}
break; //will go to the next foreach iteration, i.e. starts a new while
}
对于您的代码示例,在注释中提到的点上进行一次中断将满足您的需要(退出foreach,自然地进入下一个while迭代)。
编辑:在你发布了这个例子后,你的问题似乎不在while和foreach之间的交互上,而是在开关上:break被用作"转到循环中的下一次迭代"answers"完成这个例子和开关"的关键字。
编译器会将中断视为"开关中断"。你需要自己模仿行为:
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')
}
未经测试,但以下是一些可能的方法,可以回答您的问题:
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;
}
}
}
您需要脱离foreach
和while
,因此如下所示:
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;
}
}
}
我是C#的初学者,但我建议您在while循环中添加另一个可以自己设置的条件。然后,随时需要更改。例如:
MyKeyCondition = 0;
while(MainCondition||MyKeyCondition){
if(MyCheckCondition){
MyKeyCondition = 1;
}
}
现在,如果更改关键条件,即使主条件不满足,也可以处理while循环。
可以尝试goto;
while (true)
{
foreach(string s in checkedlistbox1.items)
{
switch(s)
{
case "1":
if( 1 > 0)
{
goto WhileOut;
}
}
}
}
WhileOut:
// some code
嵌套循环通常表示一个方法应该被拆分。
在本例中,它可以使代码更加清晰。如果将内部for
循环放入一个单独的方法中,则可以使该方法返回一个bool
。如果希望外部while
循环继续迭代,则返回true
;否则,如果希望它退出while
循环,则返回false
。
它看起来像这样:
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
作为一个例子。