home/ atoms/ sc-conditionals

SuperCollider conditionals use if(cond, {true}, {false}) and case tests condition/action pairs in order

SuperCollider’s if takes three arguments: a Boolean expression, a true-function, and a false-function — if(condition, {trueAction}, {falseAction}). The branches are actual function objects in braces, not keyword blocks, and the condition must return true or false. A case statement tests a sequence of {condition} {action} function pairs in order, executing only the first pair whose condition returns true and then stopping. A final {true} {default} pair acts as a catch-all. There are no commas between the case pairs; a single semicolon ends the whole case expression. Boolean conditions use operators such as == (equality, distinct from the single = which assigns), !=, >, <, >=, <=, and the messages .odd, .even, .isInteger, .isFloat, .and, .or.

Examples

if(100 > 50, {"yes".postln}, {"no".postln});
case
{~x == 0} {"zero".postln}
{~x < 0}  {"negative".postln}
{true}    {"other".postln};

Assessment

Write a case expression that classifies ~n as ‘zero’, ‘small positive’ (1–9), ‘large positive’ (>=10), or ‘negative’, and verify each branch. (Alternatively: post ‘low’, ‘mid’, or ‘high’ for a value below 30, 30–70, or above 70.)

“Thesyntaxforanif/elseinSCis:if(condition, {true action}, {false action}).”
corpus · a-gentle-introduction-to-supercollider-bruno-ruviaro · chunk 8
“syntax for an if/else in SC is:if(condition, {true action}, {false action}). The condition is a Boolean test (it must returntrueorfalse).”
corpus · a-gentle-introduction-to-supercollider-ruviaro-archive-org-c · chunk 8