Reputation: 49
I have a function that runs a while loop:
void run_while_loop(const float x_d, const float x_max)
{
float x = 0;
while ( x < x_max ){
// do something
x += x_d;
}
}
It is assumed that x_d < x_max.
I want to change the function so that it adds an iteration at the end with x = x_max. If the function were called as:
run_while_loop(1, 3.123)
then I want the while loop to iterate for x = 0, 1, 2, 3, and 3.123.
What is an elegant way to code this? Thank you very much.
Upvotes: 0
Views: 229
Reputation: 559
Wrap the code that is inside the while loop into a function, and call that function after the while-loop.
void run_while_loop(const float x_d, const float x_max)
{
float x = 0;
while ( x < x_max ){
do_something_function(parameters...);
x += x_d;
}
do_something_function(final_parameters...);
}
Upvotes: 4