Conditionals

Last updated: Mar 4th, 2018

Introduction

Conditional statements are used to perform different actions based on different conditions. Conditional statements are written in our code to perform different actions for different decisions.

In Cuneiform, we use the following conditional statements:

  • if to specify a block of code to be executed, if a specified condition is true.
  • else to specify a block of code to be executed, if the same condition is false.
  • elif (else if) to specify a new condition to test, if the first condition is false.

if Statement

The if statement is used to specify a block of Cuneiform code if a condition is true. Its syntax is as follows.


if (condition) {
    // block of code to be executed if the condition is true
}
                                    

Below is an example of a system that would respond with "Good morning", if the time is less than 1200h.


if (hour < 12) {
    greeting = "Good morning";
}
                                    

else Statement

The else statement is used to specify a block of code to be executed if the conditions is false. Its syntax is as follows.


if (condition) {
    // block of code to be executed if the condition is true
} else {
    // block of code to be executed if the condition is false
}
                                    

Below is an example of a system that would respond with "Good morning", if the time is less than 1200h, otherwise "Good evening".


if (hour < 12) {
    greeting = "Good morning";
} else {
    greeting = "Good evening";
}
                                    

elif Statement

The elif statement is used to specify a new condition if the first one is false. Its syntax is as follows.


if (condition1) {
    // block of code to be executed if the condition is true
} elif (condition2) {
    // block of code to be executed if condition1 is false, and condition2 is true
} else {
    // block of code to be executed if both condition1 and condition2 are false
}
                                    

Below is an example of a system that would respond with "Good morning", if the time is less than 1200h, if not, but the time is less than 1800h, greet with "Good afternoon", otherwise "Good evening".


if (hour < 10) {
    greeting = "Good morning";
} elif (hour < 20) {
    greeting = "Good day";
} else {
    greeting = "Good evening";
}
                                    

What's next?

The next section discusses loops in Cuneiform.