if statements

Hello everyone, I remember that there is a way use a '?' in an if statement, but I remember the syntax, could you help me? I tried something like:

1
2
3
4
  if(a ? b)
     cout"a";
  else
     cout<<"b";


but something is very wrong here..
The ? operator has three parts:

condition ? true-value : false-value

It works just like an if statement, except that it returns value. It is a shortcut for:

1
2
3
4
if (condition)
  x = true-value;
else
  x = false-value;
 
x = condition ? true-value : false-value;

There is a little more to it than that, but that's enough to get started.

[edit]
cout << (show_a ? "a" : "b") << "\n";
Last edited on
Topic archived. No new replies allowed.