Conditional operator ? :)
Why doesn't the following line of code compile?
number % primes[j] == 0 ? return 0 : , ;
If the number % the prime is 0, I want to return 0 to main, else I want it to continue with the function. Here's the error it gives me:
C:\My Documents\Visual Studio Projects\FunLoops\main.cpp(32) : error C2059: syntax error : 'return'
Thats not the :conditional: operator
What your refering to is the tertiary operator (compact if statement)
if (a < b)
{
return true;
}
else
return false;
is the equivelent of
return (a < b) ? true : false;
who ever gave you the idea of it being the "conditional operator?"
as to why it doesnt compile...
thats simple, your syntax is wrong.
(i assume this is what you want)
return (number % primes[j] == 0) ? 0 : 1;
oh, and in terms of efficiency, both take just as many clock cycles to compute, its just less typing to use the tertiary operator, and programmers always like having to type less :-)
-Vulcan