# Thread: Conditional operator ? :)

1. ## 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'

2. to do that you would have to do something like this....

bool x= (number % primes[j] == 0 ) ? 1:0;
if(x) return 0;
// code here for what to do if not returning yet

3. So I can't use the conditional operator?

The reason I'm trying to use it is for efficiency; I'm in a contest to generate prime numbers

Code:
```bool IsPrime(int num)
if(num==0||num==1) return true;
int divisor;
for(divisor=num/2;num%divisor!=0;divisor--);
return (divisor==1);```

5. ## 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