+ 2

I am having trouble with the syntax in the recursion example shown. I don't know what the "?" or the number "1" indicates?

n == 0 ? | 1 ... or something like this in their example the example was in shorthand and is a bit difficult to translate as I am trying to see how to calculate the expected results from a recursive expression

14th Oct 2016, 6:28 PM
Boris Ferguson
Boris Ferguson - avatar
3 Answers
+ 8
It's just a different synthax to write an if/else statement. a = (n == 0 ? 0 : 1) is equivalent to if n == 0 { a = 0 } else { a = 1 }
14th Oct 2016, 7:36 PM
Zen
Zen - avatar
+ 2
Thank you - I was going crazy looking for an example of this 'shorthand' somewhere - thanks again
15th Oct 2016, 4:43 PM
Boris Ferguson
Boris Ferguson - avatar
+ 1
It's the ternary operator taking 3 operands
18th Nov 2016, 6:39 PM
Stephen Learmonth
Stephen Learmonth - avatar