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