Sage-Code Laboratory
index<--

Selection Statements

Selection statements in structured programming are two or multiple branches or blcks of code that are executed or not depending on one or ore conditions or a set of values.

Ternary expression

A ternary expression is an expression that evaluates to a value depending on the result of another expression. In Nim, ternary expressions are implemented using the ?: operator. The syntax for a ternary expression in Nim is as follows:


expression ? value1 : value2

The expression is evaluated, and if it evaluates to true, the value1 will be returned. If the expression evaluates to false, the value2 will be returned.

Here is an example of a ternary expression in Nim:


let x = 10;
let y = x > 5 ? 10 : 20;
Note: In this example, the ternary expression will evaluate to 10 because the expression x > 5 evaluates to true.

Ternary expressions can be used to make decisions based on the value of an expression. They can also be used to assign a value to a variable depending on the value of another variable.


var ternary = if conditional == 42: true else: false

Nim Decision

A decision statement with two branches in Nim is similar to an if-else statement in other programming languages. It allows you to make a decision based on a condition and then execute one of two blocks of code.

Syntax:

The syntax for a decision statement in Nim is as follows:

#two branches
if condition:
    #true branch
    ...
else:
    #false branch
    ...

The condition can be any expression that evaluates to a boolean value. If the condition is true, the block 1 will be executed. If the condition is false, the block 2 will be executed.

decision

Conditional Execution

Example:

Here is an example of a decision statement in Nim:

#two 
if x > 0:
    print("x is greater than 0")
else:
    print("x is less than or equal to 0")

This statement will print the message "x is greater than 0" if x is greater than 0, and the message "x is less than or equal to 0" if x is less than or equal to 0.

Decision Ladder

Sometimes one condition is not good enough. When we fail first condition, maybe we need further analyses of the situation or case. Then we can link together many "if" statements in a single chain. This is sometimes called ladder because the diagram looks like one.

decision ladder

Decision Ladder

Note: A decision statement with alternative branches in Nim is similar to an if-else-if statement in other programming languages. It allows you to make a decision based on a condition and then execute one of four blocks of code.

Syntax:

The syntax for a decision statement with 4 alternative branches in Nim is as follows:


if condition:
    block 1
elif condition1:
    block 2
elif condition3:
    block 3
else:
    block 4

The condition can be any expression that evaluates to a boolean value. If the condition is true, the block 1 will be executed. If the condition is false, the next condition will be evaluated.

If the next condition is true, the block 2 will be executed. Otherwise, the next condition will be evaluated and so on. If none of the conditions are true, the block 4 will be executed.

Example

Here is an example of a decision statement with 4 alternative branches in Nim:


let input = read();

if input == 1:
    #print("Option 1")
elif input == 2:
    #print("Option 2")
elif input == 3:
    #print("Option 3")
else:
    #print("Option 4")

This program will print the message "Option 1" if the input is 1, the message "Option 2" if the input is 2, and so on. If the input is not one of the four options, the default option will be printed.

Value selector

The equivalent of a switch statement in Nim is a case statement. A case statement allows you to execute one of multiple blocks of code based on the value of an expression.

selection diagram

Selection Diagram

Syntax

The syntax for a case statement in Nim is as follows:


case expression of
    value1:
        block 1
    value2:
        block 2
    ...
else:
        block 4

The expression can be any expression that evaluates to a value. The blocks of code can be any valid Nim code. The default block will be executed if none of the other blocks match the expression.

Example1

Here is an example of a case statement in Nim:



let x = read()

case x of
    1:
        print("x is 1")
    2:
        print("x is 2")
    3:
        print("x is 3")
else:
        print("x is not 1, 2, or 3")

This program will print the message "x is 1" if x is 1, the message "x is 2" if x is 2, and so on. If x is not one of the three values, the default message will be printed.

Example2

In next example we use a range of values and a list of values to make the selection cases.


# Case switch.
let letter = read()

case letter
    of 'a':
      echo "letter is 'a'"
    of 'b', 'c':
      echo "letter is 'b' or 'c'"
    of 'd'..'h':
      echo "letter is between 'd' and 'h'"
else:
    echo "letter is another character"

Example3

Next example is not different, except that we use strings to make a selection.


echo "Read any good books lately?"

case readLine(stdin)
of "no", "No":
  echo "Go to your local library."
of "yes", "Yes":
  echo "Carry on, then."
else:
  echo "That's great; I assume."'

Read next: Repetition