public void SomeMethod( int someParam, int execTimes )
{
while( execTimes-- > 0 ) // will decrease execTimes and execute until execTimes < 1
{ // mind that the postfix -- operator will first check > 0, then decrease.
// so, it's for example 3 > 0, 2 > 0, 1 > 0, 0 !> 0 => 3 iterations.
// you can init a local var with the parameter from outside.
int localParam = someParam;
// doYourStuff here, for example mutate localParam
localParam += 20;
// in next iteration, localParam will be reset to someParam value.
// a var declared in this loop's scope will always be reset to
// its initial value in each iteration:
int someStartValue = 0;
// ... some logic mutating someStartValue;
}
}
If you want it even cleaner, you can split this up:
public void ExecNTimes( Action thingToDo, int execTimes )
{
while( execTimes-- > 0 ) thingToDo();
}
public void Logic(int parameter)
{
// your logic, will always start with same parameter value
}
public void Caller()
{
ExecNTimes( () => Logic(5), 3 ); // parameter = 5, execute 3x
}
Mind that above example uses an Action delegate, to show some different approach to Fred's answer, which of course also works.
Here, you could use the "Repeater"-Method for different logic, too, with less duplication in code. If you need it for one single method, only, I'd opt for Fred's, though.
For reference and where you can read up on why this works: