Skip to content

Comparison Operators

Joachim Stolberg edited this page Jan 4, 2023 · 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) { ...

<= (less than or equal to)

Compares the variable on the left with the value or variable on the right of the operator. Returns true when the left operand is is less than or equal to 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) { ...

>= (greater than or equal to)

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 or equal to the right one.

Example:

if(a >= b) { ...

Comparison 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) {
    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--;
  }
}