Created
February 3, 2021 13:26
-
-
Save leveled/513acdb66c07fb7e0481c5c6ca22e718 to your computer and use it in GitHub Desktop.
Control flow statements in Nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Basic if statement | |
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, "!" | |
#Basic case statement | |
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 statement using ranges | |
from strutils import parseInt | |
echo "A number please: " | |
let n = parseInt(readLine(stdin)) | |
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 #do nothing | |
#While statement | |
echo "What's your name? " | |
var name = readLine(stdin) | |
while name == "": | |
echo "Please tell me your name: " | |
name = readLine(stdin) | |
# no ``var``, because we do not declare a new variable here | |
#While statement mimicking for loop | |
echo "Counting to 10: " | |
var i = 1 | |
while i <= 10: | |
echo i | |
inc(i) # increment i by 1 | |
#For statement | |
echo "Counting to ten: " | |
for i in countup(1, 10): | |
echo i | |
echo "Counting down from 10 to 1: " | |
for i in countdown(10, 1): | |
echo i | |
#For statement with range | |
for i in 1 .. 10: | |
... | |
#For statement with backward index operator | |
for i in 0 ..< 10: | |
... # 0 .. 9 | |
#For statement with pair() indexing | |
for index, item in ["a","b"].pairs: | |
echo item, " at index ", index | |
# => a at index 0 | |
# => b at index 1 | |
#Explicit block statement for scoping | |
block myblock: | |
var x = "hi" | |
echo x # does not work either | |
#Break statements | |
block myblock: | |
echo "entering block" | |
while true: | |
echo "looping" | |
break # leaves the loop, but not the block | |
echo "still in block" | |
echo "outside the block" | |
block myblock2: | |
echo "entering block" | |
while true: | |
echo "looping" | |
break myblock2 # leaves the block (and the loop) | |
echo "still in block" # it won't be printed | |
echo "outside the block" | |
#Continue statement | |
while true: | |
let x = readLine(stdin) | |
if x == "": continue | |
echo x | |
#When statement - evaluated by compiler, useful for platform specific | |
when system.hostOS == "windows": | |
echo "running on Windows!" | |
elif system.hostOS == "linux": | |
echo "running on Linux!" | |
elif system.hostOS == "macosx": | |
echo "running on Mac OS X!" | |
else: | |
echo "unknown operating system" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment