开发者

In Objective-C, can a for-loop's condition be modified during the loop?

开发者 https://www.devze.com 2023-03-17 09:40 出处:网络
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 e

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.


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.)


You cannot change the condition as the loop is running, and the condition is evaluated each time the loop is executed. I don't know the specifics of your case, but maybe using a function instead would solve your problem? You can pass a variable to the function and based on this variable, the function can perform different checks, and return a value accordingly. Or, perhaps, you can use a break statement to end the loop if you need to stop execution during the loop.


The condition is evaluated every time. You can easily check that by using a boolean method as the condition. Something like this:

for (int i = 0; [self booleanMethod:i]; i++) {

}

The method will be executed every time.


As previously stated, the condition is re-evaluated on every loop but you cannot change the statement itself.
But it seems you're looking for something more like (assuming you don't want to go through the newly added objects and the conditions are not exclusive):

int size = [myArray count], i = 0;
while (i<size) {
    if (smallChance)
        [myArray addObject: object];
    if (evenSmallerChange) {
        [myArray removeObjectAtIndex:i];
        size--;
        i--;
    }
    i++;
}


You should use another for loop inside and check any condition and break it.

And btw, change the for loop to

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


I think what you want is:

int l = [myArray count];
for (int i=0; i<l; i++) {
    //Do anything you want here.
}

This way, the [myArray count] will be evaluated only once for the for loop.

EDIT: Sorry, din't read the second part fully. But what you can do is change the value of l conditionally.

//Inside the for loop
if(/*some condition*/) {
    l = [myArray count]; //Re-evaluate the condition.
}


In general, modifying the condition while the loop is running isn't a good pratice. The for statement is the preferred way to loop if you know how many turns you have in advance. Anyway, you can modify the condition... but consider using while instead of for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号