Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3339076
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:27:02+00:00 2026-05-18T00:27:02+00:00

I mean this: bool passed = true; for(int i = 0; i < collection.Length;

  • 0

I mean this:

bool passed = true;
for(int i = 0; i < collection.Length; i++)
{
    if(!PassesTest(collection[i]))
    {
        passed = false;
        break;
    }
}
if(passed){/*passed code*/}

requires extra variable, extra test

for(int i = 0; i < collection.Length; i++)
{
    if(!PassesTest(collection[i]))
    {
        return;
    }
}
{/*passed code*/}

neat, but requires this to be it’s own function, if this it’s self is inside a loop or something, not the most performant way of doing things. also, writing a whole extra function is a pain

if(passed){/*passed code*/}
for(int i = 0; i < collection.Length; i++)
{
    if(!PassesTest(collection[i]))
    {
        goto failed;
    }
}
{/*passed code*/}
failed: {}

great, but you have to screw around with label names and ugly label syntax

for(int i = 0; ; i++)
{
    if(!(i < collection.Length))
    {
         {/*passed code*/}
         break;
    }
    if(!PassesTest(collection[i]))
    {
        break;
    }
}

probably the nicest, but still a bit manual, kinda wasting the functionality of the for loop construct, for instance, you can’t do this with a foreach

what is the nicest way to handle this problem?

it seems to me something like this would be nice:
foreach(…)
{
…
}
finally{…} // only executed if loop ends conventionally (without break)

am I missing something? because this is a very common problem for me, and I don’t really like any of the solutions I’ve come up with.

I use c++ and C#, so solutions in either would be great.
but would also be interested in solutions in other languages. (though a design principle that avoids this in any language would be ideal)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:27:03+00:00Added an answer on May 18, 2026 at 12:27 am

    If your language doesn’t have this feature, write a function “forall,” which takes two arguments: a list and a boolean-valued function which is to be true for all elements of the list. Then you only have to write it once, and it matters very little how idiomatic it is.

    The “forall” function looks exactly like your second code sample, except that now “collection” and “passesTest” are the arguments to that function.

    Calling forall looks roughly like:

    if (forall(myList,isGood)) {
    

    which is readable.

    As an added bonus, you could implement “exists” by calling “forall” on the negated boolean function, and negating its answer. That is, “exists x P(x)” is implemented as “not forall x not P(x)”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I mean by this, is opening a file with a Java code in
I've got this code that uses winforms to get data passed between two forms
I use viewstate like this: public bool SearchClicked { get { return Session[bool]==null? false
Using a bit of code like this: static bool Intersects(Vector2 a1, Vector2 a2, Vector2
Contract.Requires(completeURL.IsUri()); // Error: Contract.Requires(completeURL != null) Can I do this without Contract.Assume()? I mean
What does this statement mean? isChecked = isChecked ^ 1; isChecked is a BOOL
I mean this module : Option I can't find it, open Option gives me
Does this mean I can't update another table from a trigger if I'm using
What does this mean: Next add reference to: MySql.Data actually I have downloaded mysql
What does this mean and why am I getting this error. Any suggestions on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.