If Statements

The basic conditional statement in Jai is similar to other languages:

if a == b {
    print("They're equal!\n"); 
} else {
    print("They're not equal!\n");

Optionally, you can put a then after the condition. This can be convenient for visual separation in some cases.

if a == b then 
  print("They're equal!\n"); 

The comparison operators are:

  • == - logical equivalence
  • != - logical inequivalence
  • < - less than
  • > - greater than
  • <= - less than or equal
  • >= - greater than or equal

An if statement can be followed by multiple else if statements to test various other conditions.

grade := 100;
if grade >= 97 {
  print("Your grade is an A+\n");
} else if grade >= 90 {
  print("Your grade is an A\n");
} else if grade >= 80 {
  print("Your grade is a B\n");
} else if grade >= 70 {
  print("Your grade is a C\n");
} else if grade >= 60 {
  print("Your grade is a D\n");
} else {
  print("You grade is a F\n");

ifx Ternary Operator Statement

Just like C++, Jai has its own ternary operator statement. ifx allows a programmer to condense a simple if statement down to a single line statement. The syntax of ifx is ifx followed by a condition statement, the value assigned if the condition is true, else, and finally the value assign if the condition is false.

a := 0;
b := 100;
c := ifx a > b 10 else 1000;
d := ifx a > b then 10 else 1000;

Case branching

The if-case statement in Jai allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable is checked for each case. This if-case statement is similar to a switch statement in C, with a few exceptions. Unlike C, there is no need to put a break statement after each case to prevent fallthrough, if there is a break statement in the if-case, the statement will attempt to break out of a loop the statement is nested in. Also unlike C, there is no need to add brackets to segregate the cases. case; will assign to the default value.

a := 0;
if a == {
case 0;
  print("case 0\n"); // because a=0, this if-case statement will print out "case 0".
case 1;
  print("case 1\n"); // because a=0, this will be ignored
  print("default case\n"); // because a=0, this print will be ignored.

Fallthrough switch behavior like in C can be obtained by adding a #through; at the end of a case statement.

a := 0;
if a == {
case 0;
  print("case 0\n"); // because a=0, this if-case statement will print out "case 0".
case 1;
  // because of the #through statement, this if-case statement will print out "case 1" 
  // in addition to "case 0". 
  print("case 1\n"); 
  print("default case\n"); // because there is no #through statement, this print will be ignored

if-case statements work on integers, strings, enums, bools, arrays, and floats. Be careful when using if-case statements with floats since floating point numbers approximate values.

The #complete compiler directive requires you to fill out all the case possibilities when using enum. This is useful when adding additional enum members to an enum. #complete only works when applied to enums or enum_flag datatypes.

Val :: enum { A; B; C; }

a := Val.A;
if #complete a == {
case Val.A;
  print("This is Val.A case\n");
case Val.B;
  print("This is Val.B case\n");
case Val.C;
  print("This is Val.C case\n");

Here is the static compile-time version of #if statements.

#if CONSTANT == 0 {

} else #if CONSTANT == 1 {

} else #if CONSTANT == 2 {