Reputation: 1196
I have a while
loop and inside this while loop
I have a foreach loop
.
Here I learned how to skip the currently interaction on a loop by using Continue; Return; Break
. But I need to leave the while loop
when i'm inside the foreach loop
is that possible ?
I'm on a interaction inside of the foreach loop
and I want to leave the foreach and go to the next interaction of the while
. How May I do that ?
Like so:
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.
}
}
}
}
Here's what I want do to:
I'm reading a file.txt line by line, and writing a new one with these values and some others things... Some of these values may be required
(seted by user), if a required field
is empty, so I do nothing, and go to the next while interaction
...
Upvotes: 1
Views: 5657
Reputation: 109557
Often nested loops are an indication that a method should be split up.
In this example, it could make the code clearer. If you place the inner for
loop into a separate method, you can make that method return a bool
. Return true
if you want the outer while
loop to continue iterating; otherwise, return false
if you want it to exit the while
loop.
It would look something like this:
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.
}
I'm not sure what you'd need to pass to processNextItem()
; I've just passed reader
as an example.
Upvotes: 0
Reputation: 7475
You need to break from the switch and then from the foreach, whilst having a variable set.
You can then check that variable to see whether you should continue
to the next while
iteration.
Do it like this:
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
}
Example of this flow working
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
}
Outputs:
1 - 0
1 - 1
Skipping switch...
Skipping foreach...
Skipping to next while...
2 - 0
2 - 1
Skipping switch...
Skipping foreach...
Skipping to next while...
Upvotes: 4
Reputation: 7829
1) Yes, it is possible. It works.
foreach() {
while() {
break; //leave the while
}
//... and continues from here
}
2) Each while will end before the next foreach iteration, so the second question does not make much sense... Unless you mean starting the next while inside the foreach, in which case.. yes!
foreach() {
while() {
...
}
break; //will go to the next foreach iteration, i.e. starts a new while
}
As for your code sample, a break in the point you mentioned in the comment will do what you need (exit the foreach, going naturally to the next while iteration).
EDIT: after you posted the example, it appears that your problem is not in the interaction between while and foreach, but in the switch: break is used as a keyword for both "go to the next iteration in the loop" and "finish this case and the switch".
The break will be seen by the compiler as a 'switch break'. You need to mimic the behavior by yourself:
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')
}
Upvotes: 1
Reputation: 19591
May try goto;
while (true)
{
foreach(string s in checkedlistbox1.items)
{
switch(s)
{
case "1":
if( 1 > 0)
{
goto WhileOut;
}
}
}
}
WhileOut:
// some code
Upvotes: 0
Reputation: 48
I'm beginner in C#, but I suggest that you add another condition to the while loop that you can set yourself. Then, change it whenever is needed. For example:
MyKeyCondition = 0;
while(MainCondition||MyKeyCondition){
if(MyCheckCondition){
MyKeyCondition = 1;
}
}
Now, if you change your key condition, you can handle the while loop even if the main condition is not satisfied.
Upvotes: 0
Reputation: 8147
You need to break from your foreach
and the while
so something like this:
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;
}
}
}
Upvotes: 0
Reputation: 2368
Not tested, but these are a few possible ways which should answer your question:
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;
}
}
}
Upvotes: 0
Reputation: 13351
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;
}
}
}
}
Upvotes: 1