ternary operator

If I do something like:

Primes[k] ? ++count : 0;

What do I put after the colons if I have nothing to put there? This is because I only want to check for Primes[k] and nothing more. I usually put some random value in there, but I don't know if this does anything in the code.

I have tried calling a void function using the above method, but when I put a random number in the spot after the colon, I get an error about types not matching or something like that.

Can someone please explain to me what to put after the colon if you have no else statements to execute?
Last edited on
In this case it wolud be more simpler and clearer to use the if statement:

if ( Primes[k] ) ++count;
What do I put after the colons if I have nothing to put there?

That depends: don you want to return from the ternary operator (in which case you must return some value), or not?

If you want to return, provide a value to return. For example,

Primes[k] ? ++count : count; (or 0, as in your case)

If you don't want to return, then throw an exception

Primes[k] ? ++count : throw std::runtime_error("Impossible!");
Last edited on
Thanks for the responses guys
Topic archived. No new replies allowed.