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
case;
  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".
  #through;
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"); 
case;
  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");
}