I am designing a artificial life simulator atm and it would be very useful if I could pass any statement as an argument. For Example.

LoopStatement(X += 2,50)//loop statement 50 times
//or
LoopStatement(X += 2 * Speed,75)//loop statement 75 times

I know the for loop has something like that. Also how would use it?
Any help would be appreciated links or explanations.

You can not pass an expression to a function, since the expression would be evaluated right before the function is called, and you would not be able to repeat it.

On the other hand, you can indeed use a for loop. Hopefully that's what you're looking for.

for ( int i=0; i < 50; X += 2, i++ ) {}; /* Perform X+=2 50 times */

You can not pass an expression to a function, since the expression would be evaluated right before the function is called, and you would not be able to repeat it.

On the other hand, you can indeed use a for loop. Hopefully that's what you're looking for.

for ( int i=0; i < 50; X += 2, i++ ) {}; /* Perform X+=2 50 times */

lol not quite but thank you anyways.

Try something like this :

void doSomething(const int updateValue, const int maxLoop){
  if(updateValue < 0 ) return;
  for(int curr = 0; curr != maxLoop; curr += updateValue){
   /* do something */
  }
}

and call it like so :

int main(){
  //stuff
   whileGameIsNotOver(){
     doSomething( player.updateSpeed(), Wall.MaxPosition());
     doSomething(2,50);  //loop statement 50 times, with increments of 2.
     doSomething(2*enemy.speed(),75);
  }
 //stuff
}
This article has been dead for over six months. Start a new discussion instead.