Debian Codegraph @

src/Examples/CsoundQt/Getting Started/Language_Features/Loops_1.csd

raw
1 /* Getting started.. Control structures
2
3 In this example, we concentrate on doing the same thing, until a specific limit is reached.
4
5 If I say:
6 Put 5 T-Shirts into your bag, then you can start with this algorithm in your head.
7 1. You take one T-Shirt and put it in the bag
8 2. You count: One
9 3. You compare one to five. Conclusion..you need more TShirts, so do it again..
10 4. You start with 1., then do 2. and 3.
11 ..
12 5. You count: Five
13 6. You compare five to five. Conclusion..done, task fullfilled!
14
15
16 But why should we do that, if Csound could do this for us?
17
18 See:
19 Conditional Values (ControlConditional - Shift+F1)
20 Program Flow Control (ControlPgmctl - Shift+F1)
21 */
22 <CsoundSynthesizer>
23 <CsOptions>
24 ;-n -m0
25 </CsOptions>
26 <CsInstruments>
27
28
29 instr 1
30 ; Lets write this in Csound-Language using the "if" statement:
31 iIndex = 0 ; Number of T-Shirts in the bag at the beginning
32 loop: ; any label
33 prints "Take a T-Shirt and put it in the bag! \n"
34 iIndex = iIndex+1 ; Count..one more
35 prints " Now we have %i T-Shirts in the bag..\n", iIndex
36 if (iIndex < 5) igoto loop ; goes back to the label "loop", if there are less then 5 T-Shirts
37 endin
38
39
40
41 instr 2
42 ; There is another way to do exactly the same by using "loop_lt":
43 iIndex = 0 ; Number of T-Shirts in the bag at the beginning
44 loop:
45 prints "Take a T-Shirt and put it in the bag! \n"
46 prints " Now we have %i T-Shirts in the bag..\n", iIndex+1
47 loop_lt iIndex, 1, 5, loop ; goes back to the label "loop", if there are less then 5 T-Shirts
48 endin
49 /*
50 The advantage of this method is, that you save typing one line, because loop_lt is increasing the counter for you. But you can't see so easily which condition is checked. Is it x<5 or x>5 or x=5 ? Here is a list of different loop-types, and their behavior:
51
52 loop_lt: x < y
53 loop_gt: x > y
54 loop_ge: x >= y
55 loop_le : x <= y
56 */
57
58 </CsInstruments>
59 <CsScore>
60 f 1 0 1024 10 1
61 i 1 0 0
62 i 2 1 1
63 e
64 </CsScore>
65 </CsoundSynthesizer>
66 ; written by Alex Hofmann (Mar. 2010) - Incontri HMT-Hannover
67 <bsbPanel>
68 <label>Widgets</label>
69 <objectName/>
70 <x>1013</x>
71 <y>279</y>
72 <width>563</width>
73 <height>397</height>
74 <visible>true</visible>
75 <uuid/>
76 <bgcolor mode="background">
77 <r>255</r>
78 <g>255</g>
79 <b>255</b>
80 </bgcolor>
81 <bsbObject version="2" type="BSBLabel">
82 <objectName/>
83 <x>57</x>
84 <y>46</y>
85 <width>80</width>
86 <height>25</height>
87 <uuid>{8f2703e4-5434-4ee1-8ddd-dd5a2a139033}</uuid>
88 <visible>true</visible>
89 <midichan>0</midichan>
90 <midicc>-3</midicc>
91 <label>Nothing here...</label>
92 <alignment>left</alignment>
93 <font>Arial</font>
94 <fontsize>10</fontsize>
95 <precision>3</precision>
96 <color>
97 <r>0</r>
98 <g>0</g>
99 <b>0</b>
100 </color>
101 <bgcolor mode="nobackground">
102 <r>255</r>
103 <g>255</g>
104 <b>255</b>
105 </bgcolor>
106 <bordermode>noborder</bordermode>
107 <borderradius>1</borderradius>
108 <borderwidth>1</borderwidth>
109 </bsbObject>
110 </bsbPanel>
111 <bsbPresets>
112 </bsbPresets>
113 <EventPanel name="" tempo="60.00000000" loop="8.00000000" x="320" y="218" width="604" height="322" visible="true" loopStart="0" loopEnd="0"> </EventPanel>