If a does not change inside loop, compiler knows what sqrt from standard library is, then it is possible that it would be evaluated only once.
Clang is smart enough to move square root evaluation outside the loop, when gcc leaves it inside (and it still only single instruction anyway).
@coder777 Your example is not entirely correct, as compiler has no right to eliminate function call in this case (as it contains a sideeffect which cannot be eliminated without changing visible program behavior). condition-expression is calculated each iteration by standard, but compiler might optimise it if it will not change program behavior. In general it is better to save result of non-trivial function into variable and not rely on compiler-dependend behavior.
1 2 3 4 5 6
{
double end = sqrt(a);
for(int i = 0; i < end; ++i) {
//...
}
}