polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Core / if.html
blob6cfaea6e71c672df5de345b6eba440baf3cf83a5
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.41">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0025b2; min-height: 12.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0021e7}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #8b2719}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0025b2}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
20 span.s1 {color: #000000}
21 span.s2 {text-decoration: underline}
22 span.s3 {color: #002dad}
23 span.s4 {color: #8b2719}
24 span.s5 {color: #0025b2}
25 span.s6 {color: #606060}
26 span.s7 {color: #416d1f}
27 span.Apple-tab-span {white-space:pre}
28 </style>
29 </head>
30 <body>
31 <p class="p1"><b>if</b></p>
32 <p class="p2"><br></p>
33 <p class="p3"><br></p>
34 <p class="p4"><b>if( boolean, trueFunc, falseFunc)</b></p>
35 <p class="p3"><br></p>
36 <p class="p3"><br></p>
37 <p class="p5"><span class="s1">see also: <a href="../Language/Control-Structures.html"><span class="s2">Control-Structures</span></a></span></p>
38 <p class="p3"><br></p>
39 <p class="p3"><br></p>
40 <p class="p3"><br></p>
41 <p class="p6"><span class="s1">if ( [<span class="s3">false</span>, <span class="s3">true</span>].choose,<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>// Boolean expression (chooses one at random)</p>
42 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span>{ "expression was true".postln },<span class="Apple-tab-span"> </span><span class="s4">// true function</span></p>
43 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span>{ "expression was false".postln }<span class="Apple-tab-span"> </span><span class="s4">// false function</span></p>
44 <p class="p8">)</p>
45 <p class="p3"><br></p>
46 <p class="p8">(</p>
47 <p class="p8"><span class="s3">var</span> a = 1, z;</p>
48 <p class="p8">z = if (a &lt; 5, { 100 },{ 200 });</p>
49 <p class="p8">z.postln;</p>
50 <p class="p8">)</p>
51 <p class="p3"><br></p>
52 <p class="p3"><br></p>
53 <p class="p4"><b>UGens can also use if</b></p>
54 <p class="p3"><br></p>
55 <p class="p4">the condition ugen is 0 / 1<span class="Apple-converted-space"> </span></p>
56 <p class="p3"><br></p>
57 <p class="p8">(</p>
58 <p class="p8"><span class="Apple-tab-span"> </span>{</p>
59 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>if( <span class="s5">LFNoise1</span>.kr(1.0,0.5,0.5) , <span class="s5">SinOsc</span>.ar, <span class="s5">Saw</span>.ar )</p>
60 <p class="p8"><span class="Apple-tab-span"> </span>}.play</p>
61 <p class="p9">)</p>
62 <p class="p3"><br></p>
63 <p class="p3"><br></p>
64 <p class="p4">ADVANCED:<span class="Apple-converted-space"> </span></p>
65 <p class="p10"><br></p>
66 <p class="p4">the functions will be inlined, which plucks the code from the functions and uses a more efficient jump statement.</p>
67 <p class="p3"><br></p>
68 <p class="p8">{</p>
69 <p class="p8"><span class="Apple-tab-span"> </span>if( 6 == 9,{</p>
70 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">"hello"</span>.postln;</p>
71 <p class="p8"><span class="Apple-tab-span"> </span>},{</p>
72 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">"hello"</span>.postln;</p>
73 <p class="p8"><span class="Apple-tab-span"> </span>})</p>
74 <p class="p8">}.def.dumpByteCodes</p>
75 <p class="p3"><br></p>
76 <p class="p8">BYTECODES: (18)</p>
77 <p class="p8"><span class="Apple-converted-space">  </span>0 <span class="Apple-converted-space">  </span><span class="s5">FE</span> 06<span class="Apple-converted-space">    </span><span class="s5">PushPosInt</span> 6</p>
78 <p class="p8"><span class="Apple-converted-space">  </span>2 <span class="Apple-converted-space">  </span><span class="s5">FE</span> 09<span class="Apple-converted-space">    </span><span class="s5">PushPosInt</span> 9</p>
79 <p class="p8"><span class="Apple-converted-space">  </span>4 <span class="Apple-converted-space">  </span>E6 <span class="Apple-converted-space">      </span>SendSpecialBinaryArithMsg <span class="s7">'=='</span></p>
80 <p class="p8"><span class="Apple-converted-space">  </span>5 <span class="Apple-converted-space">  </span><span class="s5">F8</span> 00 06 <span class="s5">JumpIfFalse</span> 6<span class="Apple-converted-space">  </span>(14)</p>
81 <p class="p8"><span class="Apple-converted-space">  </span>8 <span class="Apple-converted-space">  </span>42 <span class="Apple-converted-space">      </span><span class="s5">PushLiteral</span> <span class="s6">"hello"</span></p>
82 <p class="p8"><span class="Apple-converted-space">  </span>9 <span class="Apple-converted-space">  </span><span class="s5">A1</span> 00<span class="Apple-converted-space">    </span><span class="s5">SendMsg</span> <span class="s7">'postln'</span></p>
83 <p class="p8"><span class="Apple-converted-space"> </span>11 <span class="Apple-converted-space">  </span><span class="s5">FC</span> 00 03 <span class="s5">JumpFwd</span> 3<span class="Apple-converted-space">  </span>(17)</p>
84 <p class="p8"><span class="Apple-converted-space"> </span>14 <span class="Apple-converted-space">  </span>41 <span class="Apple-converted-space">      </span><span class="s5">PushLiteral</span> <span class="s6">"hello"</span></p>
85 <p class="p8"><span class="Apple-converted-space"> </span>15 <span class="Apple-converted-space">  </span><span class="s5">A1</span> 00<span class="Apple-converted-space">    </span><span class="s5">SendMsg</span> <span class="s7">'postln'</span></p>
86 <p class="p8"><span class="Apple-converted-space"> </span>17 <span class="Apple-converted-space">  </span><span class="s5">F2</span> <span class="Apple-converted-space">      </span><span class="s5">BlockReturn</span></p>
87 <p class="p8">a FunctionDef in closed FunctionDef</p>
88 <p class="p3"><br></p>
89 <p class="p3"><br></p>
90 <p class="p3"><br></p>
91 <p class="p3"><br></p>
92 <p class="p3"><br></p>
93 <p class="p4"><b>failure to inline due to variable declarations</b></p>
94 <p class="p8">{</p>
95 <p class="p3"><br></p>
96 <p class="p8"><span class="Apple-tab-span"> </span>if( 6 == 9,{</p>
97 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">var</span> notHere;</p>
98 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">"hello"</span>.postln;</p>
99 <p class="p8"><span class="Apple-tab-span"> </span>},{</p>
100 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">"hello"</span>.postln;</p>
101 <p class="p8"><span class="Apple-tab-span"> </span>})</p>
102 <p class="p3"><br></p>
103 <p class="p8">}.def.dumpByteCodes</p>
104 <p class="p3"><br></p>
105 <p class="p8">WARNING: <span class="s5">FunctionDef</span> contains variable declarations and so will not be inlined.</p>
106 <p class="p8"><span class="Apple-converted-space">   </span>in file 'selected text'</p>
107 <p class="p8"><span class="Apple-converted-space">   </span>line 4 char 14 :</p>
108 <p class="p8"><span class="Apple-converted-space">  <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s5">var</span> notHere;•</p>
109 <p class="p8"><span class="Apple-converted-space">  <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s6">"hello"</span>.postln;</p>
110 <p class="p8">-----------------------------------</p>
111 <p class="p8">BYTECODES: (12)</p>
112 <p class="p8"><span class="Apple-converted-space">  </span>0 <span class="Apple-converted-space">  </span><span class="s5">FE</span> 06<span class="Apple-converted-space">    </span><span class="s5">PushPosInt</span> 6</p>
113 <p class="p8"><span class="Apple-converted-space">  </span>2 <span class="Apple-converted-space">  </span><span class="s5">FE</span> 09<span class="Apple-converted-space">    </span><span class="s5">PushPosInt</span> 9</p>
114 <p class="p8"><span class="Apple-converted-space">  </span>4 <span class="Apple-converted-space">  </span>E6 <span class="Apple-converted-space">      </span>SendSpecialBinaryArithMsg <span class="s7">'=='</span></p>
115 <p class="p8"><span class="Apple-converted-space">  </span>5 <span class="Apple-converted-space">  </span>04 00<span class="Apple-converted-space">    </span><span class="s5">PushLiteralX</span> instance of <span class="s5">FunctionDef</span> in closed <span class="s5">FunctionDef</span></p>
116 <p class="p8"><span class="Apple-converted-space">  </span>7 <span class="Apple-converted-space">  </span>04 01<span class="Apple-converted-space">    </span><span class="s5">PushLiteralX</span> instance of <span class="s5">FunctionDef</span> in closed <span class="s5">FunctionDef</span></p>
117 <p class="p8"><span class="Apple-converted-space">  </span>9 <span class="Apple-converted-space">  </span>C3 0B<span class="Apple-converted-space">    </span>SendSpecialMsg <span class="s7">'if'</span></p>
118 <p class="p8"><span class="Apple-converted-space"> </span>11 <span class="Apple-converted-space">  </span><span class="s5">F2</span> <span class="Apple-converted-space">      </span><span class="s5">BlockReturn</span></p>
119 <p class="p8">a FunctionDef in closed FunctionDef</p>
120 <p class="p3"><br></p>
121 <p class="p3"><br></p>
122 <p class="p3"><br></p>
123 <p class="p3"><br></p>
124 <p class="p3"><br></p>
125 <p class="p8">{</p>
126 <p class="p8"><span class="Apple-tab-span"> </span>if( 6 == 9,{</p>
127 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">"hello"</span>.postln;</p>
128 <p class="p8"><span class="Apple-tab-span"> </span>},{</p>
129 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">"hello"</span>.postln;</p>
130 <p class="p8"><span class="Apple-tab-span"> </span>})</p>
131 <p class="p8">}.def.dumpByteCodes</p>
132 <p class="p3"><br></p>
133 <p class="p8">BYTECODES: (18)</p>
134 <p class="p8"><span class="Apple-converted-space">  </span>0 <span class="Apple-converted-space">  </span><span class="s5">FE</span> 06<span class="Apple-converted-space">    </span><span class="s5">PushPosInt</span> 6</p>
135 <p class="p8"><span class="Apple-converted-space">  </span>2 <span class="Apple-converted-space">  </span><span class="s5">FE</span> 09<span class="Apple-converted-space">    </span><span class="s5">PushPosInt</span> 9</p>
136 <p class="p8"><span class="Apple-converted-space">  </span>4 <span class="Apple-converted-space">  </span>E6 <span class="Apple-converted-space">      </span>SendSpecialBinaryArithMsg <span class="s7">'=='</span></p>
137 <p class="p8"><span class="Apple-converted-space">  </span>5 <span class="Apple-converted-space">  </span><span class="s5">F8</span> 00 06 <span class="s5">JumpIfFalse</span> 6<span class="Apple-converted-space">  </span>(14)</p>
138 <p class="p8"><span class="Apple-converted-space">  </span>8 <span class="Apple-converted-space">  </span>42 <span class="Apple-converted-space">      </span><span class="s5">PushLiteral</span> <span class="s6">"hello"</span></p>
139 <p class="p8"><span class="Apple-converted-space">  </span>9 <span class="Apple-converted-space">  </span><span class="s5">A1</span> 00<span class="Apple-converted-space">    </span><span class="s5">SendMsg</span> <span class="s7">'postln'</span></p>
140 <p class="p8"><span class="Apple-converted-space"> </span>11 <span class="Apple-converted-space">  </span><span class="s5">FC</span> 00 03 <span class="s5">JumpFwd</span> 3<span class="Apple-converted-space">  </span>(17)</p>
141 <p class="p8"><span class="Apple-converted-space"> </span>14 <span class="Apple-converted-space">  </span>41 <span class="Apple-converted-space">      </span><span class="s5">PushLiteral</span> <span class="s6">"hello"</span></p>
142 <p class="p8"><span class="Apple-converted-space"> </span>15 <span class="Apple-converted-space">  </span><span class="s5">A1</span> 00<span class="Apple-converted-space">    </span><span class="s5">SendMsg</span> <span class="s7">'postln'</span></p>
143 <p class="p8"><span class="Apple-converted-space"> </span>17 <span class="Apple-converted-space">  </span><span class="s5">F2</span> <span class="Apple-converted-space">      </span><span class="s5">BlockReturn</span></p>
144 <p class="p8">a FunctionDef in closed FunctionDef</p>
145 <p class="p3"><br></p>
146 <p class="p3"><br></p>
147 <p class="p3"><br></p>
148 <p class="p2"><br></p>
149 <p class="p2"><br></p>
150 <p class="p2"><br></p>
151 <p class="p2"><br></p>
152 </body>
153 </html>