473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

foreach loop

In a foreach loop, how do you break out of the iteration but not the
loop? Break and continue both seem to exit the loop, I need something
that for example on an if statement will cease execution of the current
iteration and move to the next one.
Any help would be really appreciated.
Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
6 15923
It works for me

string s = "Hello world";
foreach( char c in s )
{
if( c == 'l' )
continue;
Console.WriteLine(c);
}

This prints out all of the characters except the 'l's. Are you sure you have your continue at the right place in your logic?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

In a foreach loop, how do you break out of the iteration but not the
loop? Break and continue both seem to exit the loop, I need something
that for example on an if statement will cease execution of the current
iteration and move to the next one.
Any help would be really appreciated.
Nov 16 '05 #2
> In a foreach loop, how do you break out of the iteration but not the
loop? Break and continue both seem to exit the loop, I need something
that for example on an if statement will cease execution of the current
iteration and move to the next one.


Unless you're at the last element within the IEnumerable continue should
iterate to the next element and start from the top of the loop.

foreach (int i in numbers)
{
if (i < 10) continue;
}

In the example above, the loop iterates through all integers in the numbers
array. By using the continue statement in conjunction with the expression (i
< 10), the statements between continue and the end of the for body are
skipped if i is less than ten.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #3
Doh! I meant break and return not break and continue. I'm guessing
continue is like 'next' for C#?
Thanks,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
int [] ints = new int{1,2,3,4,5,6,7,8,9};
foreach(int x in ints)
{
Console.WriteLine(x); // will print all numbers
}

foreach(int x in ints)
{
break;
Console.WriteLine(x); // this row will never run
}

foreach(int x in ints)
{
if(x == 8) break; // will stop execution if 8 is found
Console.WriteLine(x);
}

foreach(int x in ints)
{
if(x % 2 == 0) continue;
Console.WriteLine(x); // will print uneven numbers
}

foreach(int x in ints)
{
if(x = 8) return; // will stop entire method if 8 is found
Console.WriteLine(x);
}

continue means stop executing the body of the for loop and return to
beginning of loop

/mortb

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
Doh! I meant break and return not break and continue. I'm guessing
continue is like 'next' for C#?
Thanks,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
Thanks for your advice everybody!
Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Thanks for your advice everybody!
Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
3
by: rhaazy | last post by:
If I want to use a FOREACH loop on an array of strings, but didn't want to include the first member,how would I do this??? Psuedo Code::: foreach(string a in myStringArray (where a.index>0))...
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
1
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach...
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.