while loop MADNESS

Started by
15 comments, last by swiftcoder 11 years, 1 month ago

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

int nbActors = gScene->getNbActors();
NxActor** actors = gScene->getActors();
while(nbActors--){

NxActor* actor = *actors++;

///render all the stuff with physics :)

}

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

Thanks

J-GREEN

Greenpanoply
Advertisement

The NxActor* actor = *actors++ line is getting a pointer to the next object in the array pointed to by actors using pointer arithmetic. The while loop is looping nbActors number of times which I'm assuming in turn specifies the number of elements in the array of NxActors pointed to by actors.

so like is this loop is going to loop until nbActors-- reaches 0 before it breaks out?

also

If i wanted to specify a specific actor in that array " *actors++ " and start looping upwards from there how would that be done? for example if i wanted to start at index 5 and then ++ from there how would that be done?

J-GREEN

Greenpanoply

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

int nbActors = gScene->getNbActors();
NxActor** actors = gScene->getActors();
while(nbActors--){

NxActor* actor = *actors++;

///render all the stuff with physics smile.png

}

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

That while-loop is an obfuscated way of writing this for-loop.


int nbActors = gScene->getNbActors();
for (int i = 0; i < nbActors; ++i)
{
    NxActor* actor = actors;
    // ...
}

the world is full of Klever Koders.

Stephen M. Webb
Professional Free Software Developer

so like is this loop is going to loop until nbActors-- reaches 0 before it breaks out?

also

If i wanted to specify a specific actor in that array " *actors++ " and start looping upwards from there how would that be done? for example if i wanted to start at index 5 and then ++ from there how would that be done?

See Bregma's post above for further insight to your OP. Regarding your further questions, consider this code snippet:


int array[10];
int* p = array; // p points to first element of the array, i.e. array[0]
 
p += 4; // p points to 5th element of array, i.e.  array[4]
 
cout<<*p; // dereference p to access the data at array[4] and print the result
 
cout<<*(p-1); // dereference p to access the data at array[3] and print the result

int* q = p++; // as p is still pointing to array[4], q points to array[5]

// and so on...
 

int nbActors = gScene->getNbActors();

NxActor** actors = gScene->getActors();


while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

Line (A) exploits that non-zero integers evaluate to true, while zero evaluates to false. The while loop will count down from the number of actors (the 'x--' decrement operator returns the current value of x, and afterwards reduces the value of x by one), until the number is zero.

Line (B) exploits the fact that items in an array are allocated contiguously (i.e. one after another), to move a pointer through the array (the 'x++' increment operator is the opposite of decrement, it returns x and then increases the value of x by one).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

So swiftcoder, thank you very much well written and that helped me indeed.

but in Line B if i wanted to start at index 4 of actors, then increment up from there, how would i write that?

I know its a simple answer I am just not understanding or thinking about it correctly.


while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

J-GREEN

Greenpanoply


// Like this

actors += 4;   // start from element 4 instead of 0
nbActors -= 4; // still got the same number though so reduce initial loop count

while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

EDIT: You need to check that nbActors (after subtracting 4) is >= 1 before you enter the while loop bit though otherwise bad things will happen.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

oh I am sorry, but i should have included that I am using the first 3 values in that array within that loop. so imagine if actors[0] - actors[4] are being used and passed to some other function or variable with in the loop. Then after I am wanting to take the rest of the actors in the array, actors[4] and upward, and increment upwards as in Line B.

How would I then increment upwards knowing i dont want to disturb actors 0-4 in the array?

for example:


while(nbActors--){ // Line A

    NxActor* actor0 = *actors[0]; 
    NxActor* actor1 = *actors[1]; 
    NxActor* actor2 = *actors[2]; 
    NxActor* actor3 = *actors[3]; 


     NxActor* actor = *actors++; // Line B     I want this line to up date every loop 
                                            // with new actors number counting upward from 4


    ///render all the stuff with physics

}
J-GREEN

Greenpanoply


How would I then increment upwards knowing i dont want to disturb actors 0-4 in the array?

Like this:



NxActor* actor0 = actors[0];
NxActor* actor1 = actors[1];
NxActor* actor2 = actors[2];
NxActor* actor3 = actors[3];

actors += 4; // start from element 4 instead of 0
nbActors -= 4; // still got the same number though so reduce initial loop count

if(nbActors > 0)
{
    while(nbActors--){ // Line A
        NxActor* actor = *actors++; // Line B     

    ///render all the stuff with physics
    }
}

Alternatively:


NxActor* actor0 = *actors++;
NxActor* actor1 = *actors++;
NxActor* actor2 = *actors++;
NxActor* actor3 = *actors++;

nbActors -= 4; // still got the same number though so reduce initial loop count

if(nbActors > 0)
{
    while(nbActors--){ // Line A
        NxActor* actor = *actors++; // Line B

    ///render all the stuff with physics
    }
}
 
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement