2019-04-13

2019-04-13  本文已影响0人  Jesse_996

编译

编译运行:
nim c -r greetings.nim
编译正式版:
nim c -d:release greetings.nim
默认编译器会输出运行时的检查,通过 -d:release关掉这些输出

基本

discard """ You can have any Nim code text commented
out inside this with no indentation restrictions.
      yes("May I ask a pointless question?") """
var x, y: int
var
  x, y: int
  # a comment can occur here too
  a, b, c: string
const input = readLine(stdin) # Error: constant expression expectedlet  
let input = readLine(stdin)   # works

流控语句

if语句

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 语句

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语句可以处理整数,其他序数类型和字符串。(将很快解释什么是序数类型。)对于整数或其他序数类型,一个范围的值也是可能的:

from strutils import parseInt

echo "A number please: "
let n = parseInt(readLine(stdin))
case n
of 0..2, 4..7: echo "The number is in the set: {0, 1, 2, 4, 5, 6, 7}"
of 3, 8: echo "The number is 3 or 8"
else: discard
上一篇 下一篇

猜你喜欢

热点阅读