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

  • Home
  • SEARCH
  • 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 6109399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:24:01+00:00 2026-05-23T14:24:01+00:00

My question is, when a for-loop is executed, does the compiler read it once,

  • 0

My question is, when a for-loop is executed, does the compiler read it once, and then begin executing the loops with the values saved? Or does it evaluate the condition multiple times as the loop is executed? For example, say I have an NSMutableArray called myArray. My for loop looks something like this:

    for (int i = 0; i < [myArray count]; i++){

[myArray addObject: object]; // this has maybe a 25% chance of happening
[myArray removeObjectAtIndex: whatever]; // also has a smaller chance of happening

What I want is, if an object is removed, for the condition to be reevaluated so the for loop doesn’t try to do anything with array objects that don’t exist, but I don’t want it to add to the count, because if it does the for loop will never end, as it will keep adding objects that add more objects, etc.

  • 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-23T14:24:02+00:00Added an answer on May 23, 2026 at 2:24 pm

    The condition in a for-loop will be evaluated each time through the loop. So adding to/removing from the NSMutable array will change how the condition is evaluated. Note that it doesn’t actually change the condition, just the results of evaluating it.

    Note that if you are calling removeObjectAtIndex that you will need to adjust your index variable so that you don’t accidentally skip over an item (i.e. if you remove the item at the current index.)

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

Sidebar

Related Questions

I have loop designed to validate the user input on a question, it was
Seeing various locking related question and (almost) always finding the 'loop because of spurious
This is a stylistic question. I want to loop twice with a variable on
First time poster here. A quick question about setting up a loop here. I
I have couple of questions about the javascript for loop. First question: for (i=0;
This might seem to be a silly question at first, but please read on.
I have a cpu-consuming code, where some function with a loop is executed many
I have a bash script that does some parallel processing in a loop. I
I have a question regarding subprocess.popen() : If supposedly the command executed is in
I once posted this question, but had no answer at all, this time, the

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.