Skip to content

Latest commit

 

History

History
172 lines (121 loc) · 3.98 KB

04-control-flow.adoc

File metadata and controls

172 lines (121 loc) · 3.98 KB

Control flow

So far in our programs every line of code was executed at some point. Control flow statements allow us to have parts of code which will be executed only if some boolean condition is satisfied.

If we think of our program as a road we can think of control flow as various branches, and we pick our path depending on some condition. For example, we will buy eggs only if their price is less than some value. Or, if it is raining, we will bring an umbrella, otherwise (else) we will bring sunglasses.

Written in pseudocode, these two examples would look like this:

if eggPrice < wantedPrice:
  buyEggs

if isRaining:
  bring umbrella
else:
  bring sunglasses

Nim syntax is very similar, as you’ll see below.

If statement

An if statement as shown above is the simplest way to branch our program.

The Nim syntax for writing if statement is:

if <condition>:     (1)
  <indented block>  (2)
  1. The condition must be of boolean type: either a boolean variable or a relational and/or logical expression.

  2. All lines following the if line which are indented two spaces make the same block and will be executed only if the condition is true.

If statements can be nested, i.e. inside one if-block there can be another if statement.

if.nim
link:{source-dir}/if.nim[role=include]
  1. The first condition is true, the second is false — inner echo is not executed.

  2. Both conditions are true and both lines are printed.

  3. The first condition is false — all lines inside of its block will be skipped, nothing is printed.

  4. Using the logical and inside of the if statement.

a is smaller than b
b is smaller than c
not only that, b is *much* smaller than c

Else

Else follows after an if-block and allows us to have a branch of code which will be executed when the condition in the if statement is not true.

else.nim
link:{source-dir}/else.nim[role=include]
d is a large number
e is a small number
Note
If you only want to execute a block if the statement is false, you can simply negate the condition with the not operator.

Elif

Elif is short for "else if", and enables us to chain multiple if statements together.

The program tests every statement until it finds one which is true. After that, all further statements are ignored.

elif.nim
link:{source-dir}/elif.nim[role=include]
f is larger than 1000
g is smaller than 1000
Note
In the case of g, even though g satisfies all three conditions, only the first branch is executed, automatically skipping all the other branches.

Case

A case statement is another way to only choose one of multiple possible paths, similar to the if statement with multiple elifs. A case statement, however, doesn’t take multiple boolean conditions, but rather any value with distinct states and a path for each possible value.

Code written with in if-elif block looking like this:

if x == 5:
  echo "Five!"
elif x == 7:
  echo "Seven!"
elif x == 10:
  echo "Ten!"
else:
  echo "unknown number"

can be written with case statement like this:

case x
of 5:
  echo "Five!"
of 7:
  echo "Seven!"
of 10:
  echo "Ten!"
else:
  echo "unknown number"

Unlike the if statement, case statement must cover all possible cases. If one is not interested in some of those cases, else: discard can be used.

case.nim
link:{source-dir}/case.nim[role=include]
  1. Even though we are interested in only three values of h, we must include this line to cover all other possible cases (all other characters). Without it, the code would not compile.

You've chosen y

We can also use multiple values for each branch if the same action should happen for more than one value.

multipleCase.nim
link:{source-dir}/multipleCase.nim[role=include]
i is odd