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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:04:38+00:00 2026-05-11T09:04:38+00:00

I’m just curious what peoples’ thoughts are on this topic. Let’s say I have

  • 0

I’m just curious what peoples’ thoughts are on this topic. Let’s say I have an Array of Objects, and I want to loop through them to see if the Objects contain certain values, and if so, I want to stop the loop. Which is better practice – a for loop with a break, or a conditional loop?

The pseudo-code in the example I have provided is for argument’s sake only (it is also in ActionScript, since that is my primary language of late). Also, I am not looking for best practice ideas on syntax.

for loop with break:

var i:int;  var isBaxterInMilwaukee:Boolean;      for (i = 0; i < arrayLen; i++) {     if (myArray[i]['name'] == 'baxter'          && myArray[i]['location'] == 'milwaukee')     {         isBaxterInMilwaukee = true;          barkTwice();          break;     } } 

conditional loop:

var i:int;  var isBaxterInMilwaukee:Boolean;      while (!isBaxterInMilwaukee && i < arrayLen) {     if (myArray[i]['name'] == 'baxter'          && myArray[i]['location'] == 'milwaukee')     {         isBaxterInMilwaukee = true;          barkTwice();     }      i++; } 
  • 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. 2026-05-11T09:04:38+00:00Added an answer on May 11, 2026 at 9:04 am

    In short, you should go with whichever version is the easiest to read and maintain.

    In slightly older times, I know breaking out of a loop was considered to be a no-no (on par with a goto statement). Loops were supposed to break on the loop condition and nowhere else. Thus, the while-loop would have been the way to go.

    (This is probably a holdover from assembly, where loops are basically a block of code with a go-to-the-beginning-if-true jump statement at the end. Multiple conditional-jump statements in the block make it exceedingly hard to debug; thus they were to be avoided and combined into one at the end.)

    I feel this idea seems to be changing a bit today, especially with foreach loops and the managed world; it’s really a matter of style now. Break-on-found for-loops have perhaps come to be acceptable to many, save some purists of course. Note that I would still avoid using break in a while-loop, however, as this can muddle the loop condition and make it confusing.

    If you’ll allow me to use a foreach loop, I consider the code below to be a lot easier to read than its while-loop brother:

    bool isBaxterInMilwaukee;      foreach (var item in myArray) {     if (item.name == 'baxter' && item.location == 'milwaukee')     {         isBaxterInMilwaukee = true;             barkTwice();         break;     } } 

    However, as the logic grows in complexity, you may want to consider a prominent comment near the break statement lest it become buried and hard to find.


    Arguably, this whole thing should be refactored into its own function which doesn’t break on found, but actually returns the result (feel free to use the for-loop version instead):

    bool isBaxterInMilwaukee(Array myArray) {           foreach (var item in myArray)     {         if (item.name == 'baxter' && item.location == 'milwaukee')         {             barkTwice();             return true;         }     }     return false; } 

    As Esko Luontola pointed out, it would probably be best to move the call to barkTwice() outside of this function as the side-effect is not evident from the function’s name, nor related to finding Baxter in every case. (Or add a boolean parameter BarkTwiceIfFound and change the line to read if(BarkTwiceIfFound) barkTwice(); to make the side-effect clear.)


    For the record, you can also do the flag check in the for-loop without a break, but I feel this actually hurts readability because you don’t expect an extra condition in a for-loop definition:

    var i:int;  var isBaxterInMilwaukee:Boolean;      for (i = 0; !isBaxterInMilwaukee && i < arrayLen; i++) {     if (myArray[i]['name'] == 'baxter'          && myArray[i]['location'] == 'milwaukee')     {         isBaxterInMilwaukee = true;             barkTwice();     } } 

    You can also simulate auto-incrementing mechanics with a while-loop. I don’t like this for a few reasons – you have to initialize i to be one less than your real starting value, and depending on how your compiler short-circuits the loop-condition logic, your value of i on exiting the loop may vary. Nevertheless, it is possible and for some people, this can improve readability:

    var i:int = -1;  var isBaxterInMilwaukee:Boolean;      while (!isBaxterInMilwaukee && ++i < arrayLen) {     if (myArray[i]['name'] == 'baxter'          && myArray[i]['location'] == 'milwaukee')     {         isBaxterInMilwaukee = true;         barkTwice();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I

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.