Skip to content

Conditional Logic

This section describes how to use logic to control program flow.

Conditional logic is used to control the flow of the program by asking questions about variables and literals.

The conditional operators work just like many other languages:

  • Less than and less than or equal: ”<”, ”<=”
  • Greater than and greater than or equal: ”>”, ”>=”
  • Equal: ”==”
  • Not equal: ”!=”

The most common use of conditional logic is in “if” statements, where you can change program behavior based on variables and literals. The code inside the ”{ }” block will run if the logic is true.

if 1 < 2 {
println("one is less than two.");
}
i32 monster_health = 5;
if monster_health == 0 {
// This code will never be executed unless monster_health is equal to 0.
println("Monster Died!");
}
if monster_health != 0 {
// If monster health is not equal to zero, print
println("Monster is still alive!");
}
i32 health = 10;
i32 low_health_warning = 20;
if health <= low_health_warning {
// if health is less than or equal to the low health warning then use potion.
println("Health is low, use healing potion.");
}
// Strings can be compared using "==" and "!="
string monster = "Giant Snake";
if monster != "Goblin" {
if monster == "Giant Snake" {
println(monster + " casts earth magic!"); // "Giant Snake casts earth magic!"
}
}
// Enumerations can also be compared using "==" and "!="
enum spell = :FIRE_BALL;
if spell != :LIGHTNING_BOLT {
if spell == :FIRE_BALL {
println("Cast Fire Ball!");
}
}
// Logic results are boolean and can be assigned to bool variables.
bool my_condition = 1 < 2; // same as: bool my_condition = true;
if my_condition { // same as: if 1 < 2 { ...
println("one is less than two.");
}

If statement chains can be used for more complex flow. Start with “if” and then create chains by adding “else if”. Finally add “else” to catch everything else.

f32 range = 24.7;
if range > 30 {
// program only enters here if range is greater than 30.
println("Long Range");
}
else if range > 20 {
// if range is not greater than 30, but it is greater than 20, enter here.
println("Medium Range");
}
else {
// if range is not greater than 30 or 20, enter here.
println("Short Range");
}

More complex questions can be asked using And ”&&”, Or ”||”, and Not ”!”.

Logic “short circuiting” is used to bypass logic rules if they are not needed. This works by checking to see if the left side of an ”&&” or ”||” is satisfied first, and skipping the right side if the answer is already clear.

f32 range = 24.7;
if range > 10 && range < 15 {
// if range is between 10 and 15, use the plasma blaster
println("Use plasma blaster.");
}
i32 health = 10;
i32 mana = 2;
if health < 5 || mana < 3 {
// if we have low health or low mana, we should use a potion.
println("Health or Mana is low, you should use a potion.");
}
// Ask a question using boolean (true or false) variables:
bool is_super_strong = true;
bool has_key = false;
bool can_lockpick; // <-- false by default
if has_key || can_lockpick || is_super_strong {
// if we have the key, we can pick locks, or we're super strong, then open chest.
println("Opened chest.");
}
// Not is used to invert logic.
if !can_lockpick {
// can_lockpick is false, so this asks: if we can't pick locks then ...
println("We can't pick this lock.");
}
// You can use parenthesis to group logic
if !(has_key || can_lockpick) && !is_super_strong {
// if not (have the key or can pick locks) -> same as: if !has_key && !can_lockpick
// and
// we are not super strong ...
println("Cannot open chest.");
}
// Complex example using many rules:
bool have_magic, hungry = true, false;
if range < 10 && ( // splitting into multiple lines can make the rules more clear.
( have_magic && mana >= 3 ) ||
( is_super_strong && !hungry )
) {
// if range is less than 10
// and
// we have magic and our mana is greater than or equal to 3
// or
// we are super strong and not hungry
println("Vanquished the monster!");
} else {
println("You should probably run!");
}

“if” conditions can be arbitrarily nested when needed, but this can make the code hard to follow.

i32 a = 1;
i32 b = 2;
i32 c = 3;
if a < b {
if c > b {
if b != b {
// never visited
}
else {
if b < b {
// never visited
}
else if b == b {
if c >= c {
println("All checks passed!");
}
else {
// never visited
}
}
else {
// never visited
}
}
}
else {
// never visited
}
}
else {
// never visited
}