-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConditionals_if-else_case.tex
81 lines (61 loc) · 4.23 KB
/
Conditionals_if-else_case.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
\section{Conditionals: if/else and case}
If it's raining, I'll take an umbrella when I go out. If it's sunny, I'll take my sunglasses. Our days are filled with this kind of decision making. In programming, these are the moments when your code has to test some condition, and take different courses of action depending on the result of the test (true or false). There are many types of conditional structures. Let's take a look at two simple ones: \emph{if/else} and \emph{case}.
The syntax for an if/else in SC is: \texttt{if(condition, \{true action\}, \{false action\})}. The condition is a Boolean test (it must return \texttt{true} or \texttt{false}). If the test returns true, the first function is evaluated; otherwise, the second function is. Try it:
\begin{lstlisting}[style=SuperCollider-IDE, basicstyle=\scttfamily\footnotesize]
// if / else
if(100 > 50, { "very true".postln }, { "very false".postln });
\end{lstlisting}
The table below, borrowed from the SuperCollider book\footnote{Cottle, D. ``Beginner's Tutorial.'' The SuperCollider Book, MIT Press, 2011, p. 33}, presents some common Boolean operators that you can use. Note the distinction between a single equal sign (\texttt{x = 10}) and two equal signs (\texttt{x == 10}). The single sign means ``\textit{assign 10 to the variable x},'' while the double sign means ``\textit{is x equal to 10?}'' Type and run some of the examples from the true or false columns, and you will actually see \texttt{true} or \texttt{false} results in the Post window.
\begin{center}
\begin{tabular}{llll}
\hline
\textbf{Symbol} & \textbf{Meaning} & \textbf{True Example} & \textbf{False Example} \\
\hline
\texttt{==} & equal to? & \texttt{10 == 10} & \texttt{10 == 99} \\
\hline
\texttt{!=} & not equal to? & \texttt{10 != 99} & \texttt{10 != 10} \\
\hline
\texttt{>} & greater than? & \texttt{10 > 5} & \texttt{10 > 99} \\
\hline
\texttt{<} & less than? & \texttt{10 < 99} & \texttt{10 < 5} \\
\hline
\texttt{>=} & greater than or equal to? & \texttt{10 >= 10}, \texttt{10 >= 3} & \texttt{10 >= 99} \\
\hline
\texttt{<=} & less than or equal to? & \texttt{10 <= 99}, \texttt{10 <= 10} & \texttt{10 <= 9} \\
\hline
\texttt{odd} & is it odd? & \texttt{15.odd} & \texttt{16.odd} \\
\hline
\texttt{even} & is it even? & \texttt{22.even} & \texttt{21.even} \\
\hline
\texttt{isInteger} & is it an integer? & \texttt{3.isInteger} & \texttt{3.1415.isInteger} \\
\hline
\texttt{isFloat} & is it a float? & \texttt{3.1415.isFloat} & \texttt{3.isFloat} \\
\hline
\texttt{and} & both conditions & \texttt{11.odd.and(12.even)} & \texttt{11.odd.and(13.even)} \\
\hline
\texttt{or} & either condition & \texttt{or(1.odd, 1.even)} & \texttt{or(2.odd, 1.even)} \\
\hline
\end{tabular}
\end{center}
The last two lines (\texttt{and}, \texttt{or}) show how to write the longer expressions in either receiver notation or functional notation.
Another useful structure is \texttt{case}. It works by defining pairs of functions to be evaluated in order until one of the tests returns true:
\texttt{case}
\texttt{\{test1\} \{action1\}}
\texttt{\{test2\} \{action2\}}
\texttt{\{test3\} \{action3\}}
\dots
\texttt{\{testN\} \{actionN\}};
The expression inside each test has to return either \texttt{true} or \texttt{false}. If test1 returns false, the program ignores action1 and moves on to test2. If it's false, action2 is also ignored and we move on to test3. If that turns out to be true, then action3 is executed, and \texttt{case} stops (no further tests or actions are executed). Note that there are no commas between functions. Simply use a semicolon at the very end to mark the end of the \texttt{case} statement.
\begin{lstlisting}[style=SuperCollider-IDE, basicstyle=\scttfamily\footnotesize]
// case
(
~num = -2;
case
{~num == 0} {"WOW".postln}
{~num == 1} {"ONE!".postln}
{~num < 0} {"negative number!".postln}
{true} {"last case scenario".postln};
)
\end{lstlisting}
Try changing the code above to get all possible results. Notice the useful (and optional) trick in the last line of \texttt{case} in the example above: since \texttt{true} always evaluate to true, you can define a ``last case scenario'' action that will always happen if all previous conditions happen to be false.
For more, check out the Control Structures Help file.