I would like to write something like this but I am not sure how.
for(int i = 0; i < ( the greater value between intA and intB ); i++)
The expression in the middle of your for statement works exactly like any if statement; the loop only continues when the expression evaluates as true. So there are a few logically equivalent ways to write what you want:
// using a ternary operator
for (int i=0; i < ((intA > intB) ? intA : intB); ++i)
{
// do stuff
}
// using a simple Boolean OR
for (int i=0; i < intA || i < intB; ++i)
{
// do stuff
}
// using a MAX macro
for (int i=0; i < MAX(intA, intB); ++i)
{
// do stuff
}
But in your specific case, none of these are ideal since the first two aren't really clear code and they all evaluate intA vs intB on each iteration through the loop. What's even better is something like:
int maxAB = MAX(intA, intB);
for (int i=0; i < maxAB; ++i)
{
// do stuff
}
maxAB variable in the last code block example.Use tha MAX macro.
MAX( a, b )
If it's not available, you can define it:
#define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
for(int i = 0; i < ((a) < (b) ? (a) : (b)); i++)
a is smaller then the value of the ternary operator is a
ifoutside theforexpression makes me think he wants to test the resulting values or something.fornotifat the start there.