OSDN Git Service

...。
[ring-lang-081/ring.git] / docs / ja-jp / build / html / _sources / controlstructures.txt
1 .. index:: 
2         single: 制御構造 - 第一形式; はじめに
3
4 ===================
5 制御構造 - 第一形式
6 ===================
7
8 第一形式による制御構造の用法を学びます。
9
10 .. index:: 
11         pair: 制御構造; 分岐処理
12
13
14 分岐処理
15 ========
16
17 * If ステートメント
18
19 文法:
20
21 .. code-block:: ring
22
23         if 式
24                 ステートメント・ブロック
25         but 式
26                 ステートメント・ブロック
27         else
28                 ステートメント・ブロック
29         ok
30
31 用例:
32
33 .. code-block:: ring
34
35         see " 
36                 Main Menu
37                 ---------
38                 (1) Say Hello
39                 (2) About
40                 (3) Exit
41
42             " give nOption
43
44         if nOption = 1  see "Enter your name : " give name see "Hello " + name + nl
45         but nOption = 2 see "Sample : using if statement" + nl
46         but nOption = 3 bye
47         else see "bad option..." + nl
48         ok
49               
50
51 .. index:: 
52         pair: 制御構造 - 第一形式; Switch ステートメント
53
54 * Switch ステートメント
55
56 文法:
57
58 .. code-block:: ring
59
60         switch 式
61         on 式
62                 ステートメント・ブロック
63         other
64                 ステートメント・ブロック
65         off
66
67 用例:
68
69 .. code-block:: ring
70
71         See " 
72                 Main Menu
73                 ---------
74                 (1) Say Hello
75                 (2) About
76                 (3) Exit
77
78             " Give nOption
79
80         Switch nOption
81         On 1 See "Enter your name : " Give name See "Hello " + name + nl
82         On 2 See "Sample : using switch statement" + nl
83         On 3 Bye
84         Other See "bad option..." + nl
85         Off
86
87
88 .. index:: 
89         pair: 制御構造 - 第一形式; ループ処理
90
91 ループ処理
92 ==========
93
94 .. index:: 
95         pair: 制御構造 - 第一形式; While ループ
96
97 * While ループ
98
99 文法:
100
101 .. code-block:: ring
102
103         while 式
104                 ステートメント・ブロック
105         end
106
107 用例:
108
109 .. code-block:: ring
110
111         While True
112
113                 See " 
114                         Main Menu
115                         ---------
116                         (1) Say Hello
117                         (2) About
118                         (3) Exit
119
120                     " Give nOption
121
122                 Switch nOption
123                 On 1 
124                         See "Enter your name : " 
125                         Give name 
126                         See "Hello " + name + nl
127                 On 2 
128                         See "Sample : using while loop" + nl
129                 On 3 
130                         Bye
131                 Other 
132                         See "bad option..." + nl
133                 Off
134         End
135
136 .. index:: 
137         pair: 制御構造 - 第一形式; For ループ
138
139 * For ループ
140
141 文法:
142
143 .. code-block:: ring
144
145         for 識別子=式 to 式 [step 式]
146                 ステートメント・ブロック
147         next
148
149 用例:
150
151 .. code-block:: ring
152
153         # 1 ~ 10 までの数値を表示します。
154         for x = 1 to 10  see x + nl  next
155
156 用例:
157
158 .. code-block:: ring
159
160         # 動的ループ
161         See "Start : " give nStart       
162         See "End   : " give nEnd
163         See "Step  : " give nStep
164         For x = nStart to nEnd Step nStep
165                 see x + nl
166         Next
167
168 用例:
169
170 .. code-block:: ring
171
172         # 0 ~ 10 までの偶数値を表示します。
173         for x = 0 to 10 step 2
174                 see x + nl
175         next
176
177 用例:
178
179 .. code-block:: ring
180
181         # 10 ~ 0 までの偶数値を表示します。
182         for x = 10 to 0 step -2
183                 see x + nl
184         next
185
186
187 .. index:: 
188         pair: 制御構造 - 第一形式; For in ループ
189
190 * For in ループ
191
192 文法:
193
194 .. code-block:: ring
195
196         for 識別子 in リストまたは文字列  [step 式]
197                 ステートメント・ブロック
198         next
199
200 用例:
201
202 .. code-block:: ring
203
204         aList = 1:10                     # 1 ~ 10 までの数値を有するリストを作成します。
205         for x in aList see x + nl  next  # 1 ~ 10 までの数値を表示します。
206
207 .. index:: 
208         pair: 制御構造 - 第一形式; Step オプション
209
210 For in での Step オプションの用法
211 =====================================
212
213 For in で Step オプションを使用することにより、繰り返しのたびに対象となった項目の数値における処理を省きます。
214
215 用例:
216
217 .. code-block:: ring
218
219         aList = 1:10    # 1 ~ 10 までの数値を有するリストを作成します。
220         # リスト内にある奇数の項目を表示します。
221         for x in aList step 2
222                 see x + nl  
223         next  
224
225
226 .. index:: 
227         pair: 制御構造 - 第一形式; For in でのリスト変更方法
228
229 For in でのリスト変更方法
230 =========================
231
232 (For in) は項目を参照で取得します。
233
234 つまり、ループの内側で項目の読み取りと編集ができます。
235         
236 用例:
237
238 .. code-block:: ring
239
240         aList = 1:5     # 1 ~ 5 までの数値を有するリストを作成します。
241         # リストの数値を文字列へ置換します。
242         for x in aList  
243                 switch x
244                 on 1  x = "one"
245                 on 2  x = "two"
246                 on 3  x = "three"
247                 on 4  x = "four"
248                 on 5  x = "five"
249                 off
250         next
251         see aList       # リストの項目を表示します。
252
253 .. index:: 
254         pair: 制御構造 - 第一形式; Do ~ Again ループ
255
256 Do ~ Again ループ
257 ==================
258
259 文法:
260
261 .. code-block:: ring
262         
263         do
264                 ステートメント・ブロック
265         again 式
266
267 用例:
268
269 .. code-block:: ring
270
271         x = 1 
272         do 
273                 see x + nl 
274                 x++ 
275         again x <= 10
276
277 .. index:: 
278         pair: 制御構造 - 第一形式; Exit 命令
279
280 Exit 命令
281 =========
282
283 一階層以上のループの外側へ脱出します。
284
285
286 文法:
287
288 .. code-block:: ring
289
290         exit  [式]     # ループの内側
291
292
293 用例:
294
295 .. code-block:: ring
296
297         for x = 1 to 10
298                 see x + nl
299                 if x = 5 exit ok
300         next
301
302 .. index:: 
303         pair: 制御構造 - 第一形式; 二階層のループからの脱出
304
305 二階層のループからの脱出
306 ========================
307
308 この用例では Exit 命令で二階層のループから一気に脱出する方法を示します。
309
310 用例:
311
312 .. code-block:: ring
313
314         for x = 1 to 10
315                 for y = 1 to 10
316                         see "x=" + x + " y=" + y + nl
317                         if x = 3 and y = 5
318                                 exit 2     # 二階層のループから脱出
319                         ok
320                 next
321         next                    
322
323 .. index:: 
324         pair: 制御構造 - 第一形式; Loop 命令
325
326 Loop 命令
327 =========
328
329 次のループの繰り返し処理を飛ばすために使用されます。
330
331 文法:
332
333 .. code-block:: ring
334
335         loop [式]       # ループの内側
336
337 用例:
338
339 .. code-block:: ring
340
341         for x = 1 to 10
342                 if x = 3
343                         see "Number Three" + nl
344                         loop
345                 ok
346                 see x + nl
347         next
348
349 .. index:: 
350         pair: 制御構造 - 第一形式; 短絡評価
351
352 短絡評価
353 ========
354
355 論理演算子 and/or は `短絡評価 <https://ja.wikipedia.org/wiki/短絡評価>`_ となります。
356
357 AND 演算子において最初の引数が 0 ならば、
358 次の引数の評価は不要であるため、結果は 0 です。
359
360 OR 演算子において最初の引数が 1 ならば、
361 次の引数の評価は不要であるため、結果は 1 です。
362
363 用例:
364
365 .. code-block:: ring
366
367         /* 実行結果
368         ** nice 
369         ** nice 
370         ** great        
371         */
372
373         x = 0 y = 10
374
375         if (x = 0 and nice()) and (y = 10 and nice())
376                 see "great" + nl
377         ok
378
379         func nice  see "nice" + nl   return 1
380
381
382 用例:
383
384 .. code-block:: ring
385
386         # 実行結果なし
387
388         x = 0 y = 10
389
390         if (x = 1 and nice()) and (y = 10 and nice())
391                 see "great" + nl
392         ok
393
394         func nice  see "nice" + nl   return 1
395
396
397 用例:
398
399 .. code-block:: ring
400
401         /* 実行結果
402         ** nice
403         ** great
404         */
405  
406         x = 0 y = 10
407
408         if (x = 0 and nice()) or (y = 10 and nice())
409                 see "great" + nl
410         ok
411
412         func nice  see "nice" + nl  return 1                    
413
414
415 .. index:: 
416         pair: 制御構造 - 第一形式; 評価方法の解説
417
418 評価方法の解説
419 ==============
420
421 * True, False, nl と NULL は言語で定義済みの変数です。
422
423 * True = 1
424
425 * False = 0
426
427 * nl = 改行
428
429 * NULL = 空文字列 = “”
430
431 * 0 (False) は以外はすべて True として評価されます。
432
433 用例:
434
435 .. code-block:: ring
436
437         # 実行結果 = message from the if statement
438
439         if 5    # 非ゼロ (0) であるため 5 は true として評価されます。
440                 see "message from the if statement" + nl
441         ok