IF is the function that gives formulas a decision. Today you evaluate IF by testing its condition and returning only the matching branch, completing the starter function set.
Evaluate IF by choosing its second or third argument based on the boolean condition.
IF is the first function that does not treat all its arguments the same. It
evaluates the condition (its first argument), and based on whether that is
true, returns either the second argument (the then-branch) or the third (the
else-branch). With A1 = 5, IF(A1>0, 1, 0) sees a true condition and returns 1;
flip A1 to -3 and the same formula returns 0. The branch value can be anything
That completes the starter function library: SUM, MIN, MAX, COUNT,
AVERAGE, and IF. The evaluator can now turn any formula the parser accepts into
a value, reading whatever cells it references. But it still evaluates on demand, in
no particular order, which is only correct when formulas read plain literals. The
moment one formula reads another formula’s result, order matters - and getting that
order right, automatically and only recomputing what changed, is the real machinery
of a spreadsheet. That is Chapter 4.
case "IF":cond := s.eval(n.Args[0])truthy := cond.Kind == Bool && cond.Bif truthy {return s.eval(n.Args[1]) // then-branch}return s.eval(n.Args[2]) // else-branch