Skip to content

Comparison Operators

Joachim Stolberg edited this page Apr 8, 2022 · 8 revisions

!= (not equal to)

Compares the variable on the left with the value or variable on the right of the operator. Returns true when the two operands are not equal.

Example:

if(a != b) { ...

< (less than)

Compares the variable on the left with the value or variable on the right of the operator. Returns true when the left operand is less then the right one.

Example:

if(a < b) { ...

== (equal to)

Compares the variable on the left with the value or variable on the right of the operator. Returns true when the two operands are equal.

Example:

if(a == b) { ...

> (greater)

Compares the variable on the left with the value or variable on the right of the operator. Returns true when the left operand is greater than the right one.

Example:

if(a  > b) { ...

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)) { ...

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

Example:

import "stdio.asm"

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

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

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

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

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