If statement

Example:

  1. var name = readLine(stdin)
  2. if name == "Andreas":
  3. echo "What a nice name!"
  4. elif name == "":
  5. echo "Don't you have a name?"
  6. else:
  7. echo "Boring name..."

The if statement is a simple way to make a branch in the control flow: The expression after the keyword if is evaluated, if it is true the corresponding statements after the : are executed. Otherwise the expression after the elif is evaluated (if there is an elif branch), if it is true the corresponding statements after the : are executed. This goes on until the last elif. If all conditions fail, the else part is executed. If there is no else part, execution continues with the next statement.

In if statements new scopes begin immediately after the if/elif/else keywords and ends after the corresponding then block. For visualization purposes the scopes have been enclosed in {| |} in the following example:

  1. if {| (let m = input =~ re"(\w+)=\w+"; m.isMatch):
  2. echo "key ", m[0], " value ", m[1] |}
  3. elif {| (let m = input =~ re""; m.isMatch):
  4. echo "new m in this scope" |}
  5. else: {|
  6. echo "m not declared here" |}