Skip to content

Boolean Operators

Joachim Stolberg edited this page Jan 4, 2023 · 9 revisions

and (logical and)

Logical AND results are true only if both operands are true. Alternatively to and the && operator can be used (as in C).

Example:

if((a == 1) and (b == 2)) { ...

or (logical or)

Logical OR results are true only if one of both operands are true. Alternatively to or the || operator can be used (as in C).

Example:

if((a == 1) or (b == 2)) { ...

Boolean Operators can only be used in if, for, and while condition.

Example:

import "sys/stdio.asm"

func init() {
  var a = 1;
  var b = 2;
  var i;

  if((a == 0) or (b == 2)) {
    putstr("ok");
  } else {
    putstr("error");
  }

  if((a == 1) and (b == 2)) {
    putstr("ok");
  } else {
    putstr("error");
  }

  for(i = 0; (i < 10) and (b == 2); i++) {
    putnum(i);
  }

  i = 10;
  while((i > 0) and (b == 2)) {
    putnum(i);
    i--;
  }
}