Ring/教程/控制結構
外觀
< Ring
在本節中,我們將學習 Ring 程式語言提供的控制結構。
- If 語句
語法
if Expression
Block of statements
but Expression
Block of statements
else
Block of statements
ok示例
see "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" give nOption
if nOption = 1 see "Enter your name : " give name see "Hello " + name + nl
but nOption = 2 see "Sample : using if statement" + nl
but nOption = 3 bye
else see "bad option..." + nl
ok- Switch 語句
語法
switch Expression
on Expression
Block of statements
other
Block of statements
off示例
See "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" Give nOption
Switch nOption
On 1 See "Enter your name : " Give name See "Hello " + name + nl
On 2 See "Sample : using switch statement" + nl
On 3 Bye
Other See "bad option..." + nl
Off
- While 迴圈
語法
while Expression
Block of statements
end示例
While True
See "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" Give nOption
Switch nOption
On 1
See "Enter your name : "
Give name
See "Hello " + name + nl
On 2
See "Sample : using while loop" + nl
On 3
Bye
Other
See "bad option..." + nl
Off
End- For 迴圈
語法
for identifier=expression to expression [step expression]
Block of statements
next示例
# print numbers from 1 to 10
for x = 1 to 10 see x + nl next示例
# Dynamic loop
See "Start : " give nStart
See "End : " give nEnd
See "Step : " give nStep
For x = nStart to nEnd Step nStep
see x + nl
Next示例
# print even numbers from 0 to 10
for x = 0 to 10 step 2
see x + nl
next示例
# print even numbers from 10 to 0
for x = 10 to 0 step -2
see x + nl
next- For in 迴圈
語法
for identifier in List/String [step expression]
Block of statements
next示例
aList = 1:10 # create list contains numbers from 1 to 10
for x in aList see x + nl next # print numbers from 1 to 10我們可以在 For in 中使用 Step 選項來跳過每次迭代中的多個項。
示例
aList = 1:10 # create list contains numbers from 1 to 10
# print odd items inside the list
for x in aList step 2
see x + nl
next當我們使用 (For in) 時,我們會按引用獲取專案。
這意味著我們可以在迴圈中讀取/編輯專案。
示例
aList = 1:5 # create list contains numbers from 1 to 5
# replace list numbers with strings
for x in aList
switch x
on 1 x = "one"
on 2 x = "two"
on 3 x = "three"
on 4 x = "four"
on 5 x = "five"
off
next
see aList # print the list items語法
do
Block of statements
again expression示例
x = 1
do
see x + nl
x++
again x <= 10
用於跳出迴圈。
語法
exit [expression] # inside loop
示例
for x = 1 to 10
see x + nl
if x = 5 exit ok
next以下示例展示瞭如何使用 exit 命令從兩個迴圈中一步跳出。
示例
for x = 1 to 10
for y = 1 to 10
see "x=" + x + " y=" + y + nl
if x = 3 and y = 5
exit 2 # exit from 2 loops
ok
next
next- Loop 命令
用於跳到迴圈的下一輪迭代。
語法
loop [expression] # inside loop示例
for x = 1 to 10
if x = 3
see "Number Three" + nl
loop
ok
see x + nl
next當我們在迴圈中時,我們可以呼叫一個函式,然後在該函式中使用 exit 和/或 loop 命令,該命令將作用於外部迴圈。
示例
# print numbers from 1 to 10 except number 5.
for x = 1 to 10
ignore(x,5)
see x + nl
next
func ignore x,y
if x = y
loop
ok
邏輯運算子 and/or 遵循`短路求值 <http://en.wikipedia.org/wiki/Short-circuit_evaluation>`_。
如果 AND 運算子的第一個引數為零,則無需計算第二個引數,結果將為零。
如果 OR 運算子的第一個引數為一,則無需計算第二個引數,結果將為一。
示例
/* output
** nice
** nice
** great
*/
x = 0 y = 10
if (x = 0 and nice()) and (y = 10 and nice())
see "great" + nl
ok
func nice see "nice" + nl return 1示例
# No output
x = 0 y = 10
if (x = 1 and nice()) and (y = 10 and nice())
see "great" + nl
ok
func nice see "nice" + nl return 1示例
/* output
** nice
** great
*/
x = 0 y = 10
if (x = 0 and nice()) or (y = 10 and nice())
see "great" + nl
ok
func nice see "nice" + nl return 1- True、False、nl & NULL 是語言定義的變數。
- True = 1
- False = 0
- nl = 換行符
- NULL = 空字串 = ""
- 除了 0 (False) 之外,所有內容都求值為真。
示例
# output = message from the if statement
if 5 # 5 evaluates to true because it's not zero (0).
see "message from the if statement" + nl
ok