-1

I was just reading some articles on Wikipedia involving some code in C++, but I was unfamiliar with one of the operators used, ?. Here's the context in which it was used:

unsigned int factorial(unsigned int n) {
    return n == 0 ? 1 : n * factorial(n - 1); 
}
  • 1
    google "conditional operator" or "ternary operator" – M.M Jul 30 '15 at 23:58
  • https://en.wikipedia.org/wiki/%3F: – childofsoong Jul 30 '15 at 23:59
  • Same as IF statement. It means .. if(n==0) return 1; else return (n * factorial(n - 1)); – Sung Jul 31 '15 at 00:01
  • Excuse me, but there's no need to downvote this question. I did look to see if it had already been answered, but the problem was I was using the word operator, so the answer was excluded. – lorentzfactor Jul 31 '15 at 01:56
  • For what little it's worth, even googling "c++ question mark" gets useful results right away (including that other stackoverlfow question as the first result). – TheUndeadFish Jul 31 '15 at 02:25

1 Answers1

1
condition ? true-outcome : false-outcome

Same as:

if (n == 0) {
    return 1;
} else {
    return n * factorial(n - 1);
}
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112