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 8166541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:54:25+00:00 2026-06-06T19:54:25+00:00

I saw a similar piece of perl code in the code base and wanted

  • 0

I saw a similar piece of perl code in the code base and wanted to know if this (setting i=100) was an OK way to get out of the for loop? Are there any pitfalls to this?

int a[100];

...

bool check_if_array_contains_29()
{
    bool result = false;
    for(int i=0; i<100; ++i)
    {
        if(a[i] == 29)
        {
            result = true;
            i = 101;
        }
    }
    return result;
}

This is more like what I would do.

bool check_if_array_contains_29()
{
    bool result = false;
    for(int i=0; i<100 && !result; ++i)
    {
        if(a[i] == 29)
        {
            result = true;
        }
    }
    return result;
}

Edit -1:

I am not looking for a oneliner in perl to achieve the functionality. The real code (functionality) was much more complex. This is just an example that I simplified to explain my point(early termination of for loop).

  • 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-06-06T19:54:28+00:00Added an answer on June 6, 2026 at 7:54 pm

    Why wouldn’t you just do this:

    bool check_if_array_contains_29()
    {
        for(int i=0; i < 100; ++i)
        {
             if (a[i] == 29)
               return true;
        }
        return false;
    }
    

    Edit:
    I know some people feel that multiple return statements are just horrible and should be avoided at all costs, but to me, having multiple returns in a method like the one presented makes the code easier to read and follow.

    Edit 2:
    Additional versions so that if the method needs to have some side effects or perform some additional operations you can use a break statement, or you can adjust the for loop conditional, or you could add some labels and some gotos.

    bool check_if_array_contains_29_take2()
    {
        bool result = false;
        for (int i=0; i < 100; ++i)
        {
            if (a[i] == 29)
            {
                result = true;
                break;
            }
        }
    
        // Do Other Stuff
        return result;
    }
    
    bool check_if_array_contains_29_take3()
    {
        bool result = false;
        for (int i=0; !result && i < 100; ++i)
        {
            result = a[i] == 29;
        }
    
        // Do Other Stuff
        return result;
    }
    
    // Special edition for those who can't get enough goto
    bool check_if_array_contains_29_and_do_more_stuff_without_early_return()
    {
        bool result = false;
        for (int i=0; i < 100; ++i)
        {
            if (a[i] == 29)
            {
                result = true;
                break;
            }
        }
    
        if (!result)
            goto error;
    
        // Do some other stuff in the code here
        goto done;
    
    done:
        return result;
    error:
        result = false;
        goto done;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw code similar to this in the Mac OS SDK: enum { kAudioFileStreamProperty_ReadyToProducePackets
I saw this similar question here but can't figure out how to use Contains
I saw similar types of posts here.But i am not getting this right.To get
Curious what "Toast" means? Saw this and am curious... Similar Posts How to add
I saw some similar question, none about mysql... Is there any way to do
I saw a similar post that was trying to do this same thing with
I'm searching for this for quite some time now. I saw few similar questions
I saw there are similar question to this. But I couldn't find an answer.
I saw some similar questions to this but none seems to address this is
I saw a similar post to this, so I tried to follow the answer

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.