let name = readLine(stdin) if name == "": echo "Poor soul, you lost your name?" elif name == "name": echo "Very funny, your name is name." else: echo "Hi, ", name, "!"
Case 语句
1 2 3 4 5 6 7 8 9 10
let name = readLine(stdin) case name of"": echo "Poor soul, you lost your name?" of"name": echo "Very funny, your name is name." of"Dave", "Frank": # 对于分支允许使用逗号分隔的值列表 echo "Cool name!" else: echo "Hi, ", name, "!"
case 语句可以处理整型、其它序数类型和字符串。对整型或序数类型值,也可以用范围:
1 2 3 4 5 6 7 8
from strutils import parseInt
echo "A number please: " let n = parseInt(readLine(stdin)) case n of0..2, 4..7: echo "The number is in the set: {0, 1, 2, 4, 5, 6, 7}" of3, 8: echo "The number is 3 or 8" else: discard# 空 discard 语句 => 什么都不做
While 语句
1 2 3 4 5
echo "What's your name? " var name = readLine(stdin) while name == "": echo "Please tell me your name: " name = readLine(stdin)
For 语句
1 2 3 4 5 6 7 8 9 10
echo "Counting to ten: " for i in countup(1, 10): # 倒数 countdown(10, 1) echo i # --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines for i in1..10: # 1..10 for i in0..<10: # 零索引计数 0..9 -> for i in 0..<s.len for index, item in ["a", "b"].pairs: echo item, " at index ", index # => a at index 0 # => b at index 1
作用域和块语句
1 2 3 4 5 6
whilefalse: var x = "hi" echo x # 不行 block myblock: var x = "hi" echo x # 不行
# 嵌套 if 语句需要缩进: if x: if y: y = false else: y = true
# 需要缩进, 因为条件后有两个语句: if x: x = false y = false
表达式为了更好的可读性可以在某些地方缩进:
1 2 3 4
if thisIsaLongCondition() and thisIsAnotherLongCondition(1, 2, 3, 4): x = true
用小括号和分号 (;) 可以在只允许表达式的地方使用语句:
1
const fac4 = (var x = 1; for i in1..4: x *= i; x)
过程(procedure)
1 2 3 4 5 6 7 8 9 10 11 12
proc yes(question: string): bool = echo question, " (y/n)" whiletrue: case readLine(stdin) of"y", "Y", "yes", "Yes": returntrue of"n", "N", "no", "No": returnfalse else: echo "Please be clear: yes or no"
if yes("Should I delete all your important files?"): echo "I'm sorry Dave, I'm afraid I can't do that." else: echo "I think you know what the problem is just as well as I do."
Result 变量
一个返回值的过程有一个隐式 result 变量声明代表返回值。一个没有表达式的 return 语句是 return result 的简写。 result 总在过程的结尾自动返回如果退出时没有 return 语句.
1 2 3 4 5 6 7 8 9
proc sumTillNegative(x: varargs[int]): int = for i in x: if i < 0: return result = result + i
type IntArray = array[0..5, int] # 一个索引为 0..5 的数组 var x: IntArray x = [1, 2, 3, 4, 5, 6] for i in low(x)..high(x): # 最小/大索引 echo x[i]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
type Direction = enum north, east, south, west BlinkLights = enum off, on, slowBlink, mediumBlink, fastBlink LevelSetting = array[north..west, BlinkLights] var level: LevelSetting level[north] = on level[south] = slowBlink level[east] = fastBlink echo repr(level) # --> [on, fastBlink, slowBlink, off] echo low(level) # --> north echo len(level) # --> 4 echo high(level) # --> west
1 2 3 4 5 6 7 8 9 10 11 12
type LightTower = array[1..10, array[north..west, BlinkLights]] type IntArray = array[0..5, int] # 一个索引为0..5的数组 QuickArray = array[6, int] # 一个索引为0..5的数组 var x: IntArray y: QuickArray x = [1, 2, 3, 4, 5, 6] y = x for i in low(x)..high(x): echo x[i], y[i]
序列
序列类似数组但是动态长度,可以在运行时改变。因为序列是大小可变的它们总是分配在堆上,被垃圾回收。
序列总是以从零开始的 int 类型索引。 len , low 和 high 操作符也可用于序列。 x[i] 标记可以用于访问 x 的第i个元素。