OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / en / source / controlstructures.txt
1 .. index:: 
2         single: Control Structures - First Style; Introduction
3
4 ================================
5 Control Structures - First Style
6 ================================
7
8 In this chapter we are going to learn about the control structures provided by
9 the Ring programming language.
10
11 .. index:: 
12         pair: Control Structures; Branching
13
14
15 Branching
16 =========
17
18 * If Statement
19
20 Syntax:
21
22 .. code-block:: ring
23
24         if Expression
25                 Block of statements
26         but Expression
27                 Block of statements
28         else
29                 Block of statements
30         ok
31
32 Example:
33
34 .. code-block:: ring
35
36         see " 
37                 Main Menu
38                 ---------
39                 (1) Say Hello
40                 (2) About
41                 (3) Exit
42
43             " give nOption
44
45         if nOption = 1  see "Enter your name : " give name see "Hello " + name + nl
46         but nOption = 2 see "Sample : using if statement" + nl
47         but nOption = 3 bye
48         else see "bad option..." + nl
49         ok
50               
51
52 .. index:: 
53         pair: Control Structures - First Style; Switch Statement
54
55 * Switch Statement
56
57 Syntax:
58
59 .. code-block:: ring
60
61         switch Expression
62         on Expression
63                 Block of statements
64         other
65                 Block of statements
66         off             
67
68 Example:
69
70 .. code-block:: ring
71
72         See " 
73                 Main Menu
74                 ---------
75                 (1) Say Hello
76                 (2) About
77                 (3) Exit
78
79             " Give nOption
80
81         Switch nOption
82         On 1 See "Enter your name : " Give name See "Hello " + name + nl
83         On 2 See "Sample : using switch statement" + nl
84         On 3 Bye
85         Other See "bad option..." + nl
86         Off
87
88
89 .. index:: 
90         pair: Control Structures - First Style; Looping
91
92 Looping
93 =======
94
95 .. index:: 
96         pair: Control Structures - First Style; While Loop
97
98 * While Loop
99
100 Syntax:
101
102 .. code-block:: ring
103
104         while Expression
105                 Block of statements
106         end
107
108 Example:
109
110 .. code-block:: ring
111
112         While True
113
114                 See " 
115                         Main Menu
116                         ---------
117                         (1) Say Hello
118                         (2) About
119                         (3) Exit
120
121                     " Give nOption
122
123                 Switch nOption
124                 On 1 
125                         See "Enter your name : " 
126                         Give name 
127                         See "Hello " + name + nl
128                 On 2 
129                         See "Sample : using while loop" + nl
130                 On 3 
131                         Bye
132                 Other 
133                         See "bad option..." + nl
134                 Off
135         End
136
137 .. index:: 
138         pair: Control Structures - First Style; For Loop
139
140 * For Loop
141
142 Syntax:
143
144 .. code-block:: ring
145
146         for identifier=expression to expression [step expression]
147                 Block of statements
148         next
149
150 Example:
151
152 .. code-block:: ring
153
154         # print numbers from 1 to 10
155         for x = 1 to 10  see x + nl  next
156
157 Example:
158
159 .. code-block:: ring
160
161         # Dynamic loop
162         See "Start : " give nStart       
163         See "End   : " give nEnd
164         See "Step  : " give nStep
165         For x = nStart to nEnd Step nStep
166                 see x + nl
167         Next
168
169 Example:
170
171 .. code-block:: ring
172
173         # print even numbers from 0 to 10
174         for x = 0 to 10 step 2
175                 see x + nl
176         next
177
178 Example:
179
180 .. code-block:: ring
181
182         # print even numbers from 10 to 0
183         for x = 10 to 0 step -2
184                 see x + nl
185         next
186
187
188 .. index:: 
189         pair: Control Structures - First Style; For In Loop
190
191 * For in Loop
192
193 Syntax:
194
195 .. code-block:: ring
196
197         for identifier in List/String  [step expression]
198                 Block of statements
199         next
200
201 Example:
202
203 .. code-block:: ring
204
205         aList = 1:10    # create list contains numbers from 1 to 10
206         for x in aList  see x + nl  next  # print numbers from 1 to 10
207
208 .. index:: 
209         pair: Control Structures - First Style; Step Option
210
211 Using The Step option with For in
212 =================================
213
214 We can use the Step option with For in to skip number of items in each iteration
215
216 Example:
217
218 .. code-block:: ring
219
220         aList = 1:10    # create list contains numbers from 1 to 10
221         # print odd items inside the list
222         for x in aList step 2
223                 see x + nl  
224         next  
225
226
227 .. index:: 
228         pair: Control Structures - First Style; for in to modify lists
229
230 Using For in to modify lists
231 =============================
232
233 When we use (For in) we get items by reference.
234
235 This means that we can read/edit items inside the loop.
236         
237 Example:
238
239 .. code-block:: ring
240
241         aList = 1:5     # create list contains numbers from 1 to 5
242         # replace list numbers with strings
243         for x in aList  
244                 switch x
245                 on 1  x = "one"
246                 on 2  x = "two"
247                 on 3  x = "three"
248                 on 4  x = "four"
249                 on 5  x = "five"
250                 off
251         next
252         see aList       # print the list items
253
254 .. index:: 
255         pair: Control Structures - First Style; Do Again Loop
256
257 Do Again Loop
258 =============
259
260 Syntax:
261
262 .. code-block:: ring
263         
264         do
265                 Block of statements
266         again expression
267
268 Example:
269
270 .. code-block:: ring
271
272         x = 1 
273         do 
274                 see x + nl 
275                 x++ 
276         again x <= 10
277
278 .. index:: 
279         pair: Control Structures - First Style; Exit
280
281 Exit Command
282 ============
283
284 Used to go outside one or more of loops.
285
286
287 Syntax:
288
289 .. code-block:: ring
290
291         exit [expression]       # inside loop
292
293
294 Example:
295
296 .. code-block:: ring
297
298         for x = 1 to 10
299                 see x + nl
300                 if x = 5 exit ok
301         next
302
303 .. index:: 
304         pair: Control Structures - First Style; Exit from two loops
305
306 Exit from two loops
307 ===================
308
309 The next example presents how to use the exit command to exit from two loops in one jump.
310
311 Example:
312
313 .. code-block:: ring
314
315         for x = 1 to 10
316                 for y = 1 to 10
317                         see "x=" + x + " y=" + y + nl
318                         if x = 3 and y = 5
319                                 exit 2     # exit from 2 loops 
320                         ok
321                 next
322         next                    
323
324 .. index:: 
325         pair: Control Structures - First Style; Loop Command
326
327 Loop Command
328 ============
329
330 Used to jump to the next iteration in the loop.
331
332 Syntax:
333
334 .. code-block:: ring
335
336         loop [expression]       # inside loop
337
338 Example:
339
340 .. code-block:: ring
341
342         for x = 1 to 10
343                 if x = 3
344                         see "Number Three" + nl
345                         loop
346                 ok
347                 see x + nl
348         next
349
350 .. index:: 
351         pair: Control Structures - First Style; Short-circuit evaluation
352
353 Short-circuit evaluation
354 ========================
355
356 The logical operators and/or follow the `short-circuit evaluation <http://en.wikipedia.org/wiki/Short-circuit_evaluation>`_.
357
358 If the first argument of the AND operator is zero, then there is no need to evaluate the second
359 argument and the result will be zero.
360
361 If the first argument of the OR operator is one, then there is no need to evaluate the second
362 argument and the result will be one.
363
364 Example:
365
366 .. code-block:: ring
367
368         /* output
369         ** nice 
370         ** nice 
371         ** great        
372         */
373
374         x = 0 y = 10
375
376         if (x = 0 and nice()) and (y = 10 and nice())
377                 see "great" + nl
378         ok
379
380         func nice  see "nice" + nl   return 1
381
382
383 Example:
384
385 .. code-block:: ring
386
387         # No output
388
389         x = 0 y = 10
390
391         if (x = 1 and nice()) and (y = 10 and nice())
392                 see "great" + nl
393         ok
394
395         func nice  see "nice" + nl   return 1
396
397
398 Example:
399
400 .. code-block:: ring
401
402         /* output 
403         ** nice
404         ** great
405         */
406  
407         x = 0 y = 10
408
409         if (x = 0 and nice()) or (y = 10 and nice())
410                 see "great" + nl
411         ok
412
413         func nice  see "nice" + nl  return 1                    
414
415
416 .. index:: 
417         pair: Control Structures - First Style; Comments about evaluation
418
419 Comments about evaluation
420 =========================
421
422 * True, False, nl & NULL are variables defined by the language
423
424 * True = 1
425
426 * False = 0 
427
428 * nl = new line
429
430 * NULL = empty string = ""
431
432 * Everything evaluates to true except 0 (False).
433
434 Example:
435
436 .. code-block:: ring
437
438         # output = message from the if statement
439
440         if 5    # 5 evaluates to true because it's not zero (0).
441                 see "message from the if statement" + nl
442         ok