Skip to content

Comparison Operators

Joachim Stolberg edited this page Apr 16, 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) { ...

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--;
  }
  return;
}