foreach (string s in sList)
{
if (s.equals("ok"))
return true;
}
return false;
Alternatively, if you need to do some other things after you've found the item:
bool found = false;
foreach (string s in sList)
{
if (s.equals("ok"))
{
found = true;
break; // get out of the loop
}
}
// do stuff
return found;
Answer from mbillard on Stack Overflowforeach (string s in sList)
{
if (s.equals("ok"))
return true;
}
return false;
Alternatively, if you need to do some other things after you've found the item:
bool found = false;
foreach (string s in sList)
{
if (s.equals("ok"))
{
found = true;
break; // get out of the loop
}
}
// do stuff
return found;
Use break; and this will exit the foreach loop
I think for simple loops, such as these, the standard first syntax is much clearer. Some people consider multiple returns confusing or a code smell, but for a piece of code this small, I do not believe this is a real issue.
It gets a bit more debatable for more complex loops. If the loop's contents cannot fit on your screen and has several returns in the loop, there is an argument to be made that the multiple exit points can make the code more difficult to maintain. For example, if you had to ensure some state maintenance method ran before exiting the function, it would be easy to miss adding it to one of the return statements and you would cause a bug. If all the end conditions can be checked in a while loop, you only have one exit point and can add this code after it.
That said, with loops especially it is good to try and put as much logic as possible into separate methods. This avoids a lot of cases where the second method would have advantages. Lean loops with clearly separated logic will matter more than which of these styles you use. Also, if most of your application's code base is using one style, you should stick with that style.
This is easy.
Almost nothing matters more than clarity to the reader. The first variant I found incredibly simple and clear.
The second 'improved' version, I had to read several times and make sure all the edge conditions were right.
There is ZERO DOUBT which is better coding style (the first is much better).
Now - what is CLEAR to people may vary from person to person. I'm not sure there are any objective standards for that (though posting to a forum like this and getting a variety of peoples inputs can help).
In this particular case, however, I can tell you why the first algorithm is more clear: I know what the C++ iterate over a container syntax looks like and does. I've internalized it. Someone UNFAMILIAR (its new syntax) with that syntax might prefer the second variation.
But once you know and understand that new syntax, its a basic concept you can just use. With the loop iteration (second) approach, you have to carefully check that the user is CORRECTLY checking for all the edge conditions to loop over the entire array (e.g. less than in stead of less-or-equal, same index used for test and for indexing etc).
jsp - JSTL continue, break inside foreach - Stack Overflow
Re: How to break a loop in JSTL c:foreach- ...
How to break out of a foreach loop? - Free Support Forum - aspose.com
Need help with nested loop (c:forEach) tags. Can I break from inner loop? - Oracle Forums
Videos
There's no such thing. Just do the inverse for the content you actually want to display. So don't do
<c:forEach items="${requestScope.DetailList}" var="list">
<c:if test="${list.someType eq 'aaa' or list.someType eq 'AAA'}">
<<<continue>>>
</c:if>
<p>someType is not aaa or AAA</p>
</c:forEach>
but rather do
<c:forEach items="${requestScope.DetailList}" var="list">
<c:if test="${not (list.someType eq 'aaa' or list.someType eq 'AAA')}">
<p>someType is not aaa or AAA</p>
</c:if>
</c:forEach>
or
<c:forEach items="${requestScope.DetailList}" var="list">
<c:if test="${list.someType ne 'aaa' and list.someType ne 'AAA'}">
<p>someType is not aaa or AAA</p>
</c:if>
</c:forEach>
Please note that I fixed an EL syntax error in your code as well.
I solved it using Set at the end of my executable code and inside of the loop
<c:set var="continueExecuting" scope="request" value="false"/>
then I used that variable to skip the execution of the code in the next iteration using
<c:if test="${continueExecuting}">
you can set it back to true at any time...
<c:set var="continueExecuting" scope="request" value="true"/>
more on this tag at: JSTL Core Tag
enjoy!
You can use std::any_of (or std::all_of or std::none_of) e.g. like this:
std::vector<int> a;
// ...
std::all_of(a.begin(), a.end(), & {
// return false if you want to break, true otherwise
});
However, this is a wasteful solution (return values are not really used for anything), and you're better off writing you own loop.
You can use std::find_if algorithm, which will stop and return the iterator to the first element where the predicate condition applied to returns true. So your predicate should be changed to return a boolean as the continue/break condition.
However, this is a hack, so you can use the algorithms.
Another way is to use BOOST_FOREACH.
So I have this function that is supposed to go through a nodelist of divs being hovered and when clicked it should check if any of the squares were already checked. If they are I want to break out of the function but because i'm inside a forEach loop the return doesn't do anything. How can I check the list and break out of the function.