Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (divide)
      (equal)
      (less)
      (minus)
      (more)
      (multiply)
      (plus)
      (power)
      Abs
      And
      Append
      Asc
      Atn
      Auto
      Backup
      Bank
      Begin
      Bend
      Bload
      Boot
      Box
      Bsave
      Bump
      Catalog
      Char
      Chr
      Circle
      Close
      Clr
      Cmd
      Collect
      Collision
      Color
      Concat
      Cont
      Copy
      Cos
      Data
      Dclear
      Dclose
      Dec
      Def
      Delete
      Dim
      Directory
      Dispose
      Dload
      Do
      Dopen
      Draw
      Ds
      Ds string
      Dsave
      Dverify
      El
      Else
      End
      Envelope
      Er
      Err
      Exit
      Exp
      Fast
      Fetch
      Filter
      Fn
      For
      Fre
      Get
      Get num
      Getkey
      Go
      Gosub
      Goto
      Graphic
      Gshape
      Header
      Help
      Hex
      If
      Input
      Input num
      Instr
      Int
      Joy
      Key
      Left
      Len
      Let
      List
      Load
      Locate
      Log
      Loop
      Mid
      Monitor
      Movspr
      New
      Next
      Not
      Off
      On
      Open
      Or
      Paint
      Peek
      Pen
      Pi
      Play
      Pointer
      Poke
      Pos
      Pot
      Print
      Print num
      Pudef
      Quit
      Rclr
      Rdot
      Read
      Record
      Rem
      Rename
      Renumber
      Restore
      Resume
      Return
      Rgr
      Right
      Rlum
      Rnd
      Rreg
      Rspcolor
      Rsppos
      Rsprite
      Run
      Rwindow
      Save
      Scale
      Scnclr
      Scratch
      Sgn
      Sin
      Sleep
      Slow
      Sound
      Spc
      Sprcolor
      Sprdef
      Sprite
      Sprsav
      Sqr
      Sshape
      St
      Stash
      Step
      Stop
      Str
      Swap
      Sys
      Tab
      Tan
      Tempo
      Then
      Ti
      Ti string
      To
      Trap
      Troff
      Tron
      Until
      Using
      Usr
      Val
      Verify
      Vol
      Wait
      While
      Width
      Window
      Xor
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
<noneB31.0 to 7.0Operator (4)

 Syntax 
subject comparator
 ~ or ~
subject < = comparator
 ~ or ~
subject = < comparator
 ~ or ~
subject > comparator
 ~ or ~
subject > comparator
 
ParametersTypeLegal Value(s)Default ValueNote(s)
subjectNumeric or String
any
comparatorNumeric or Stringany
Type must match subject (numeric or string)
 
ReturnsTypeValue(s)Note(s)
resultBoolean
-1 (true) or 0 (false)
 
 Purpose 
Evaluate a comparative relation.

 
 Remarks 
The less operator returns a boolean result; it may be used in one of three ways:
  • In the first Syntax shown above, known as "less than", if the subject is less than the comparator then -1 (for true) is returned; otherwise 0 (for false) is returned.  Hopefully it is obvious how this works for numeric values.  For comparison of strings, the pair of PETSCII codes from each corresponding position of the two strings is compared.  The first code pair not equal between the two strings will return the result, which is simply the numeric relation of the two different codes.  If all codes match up to the shorter of the two strings, then the shorter string is considered "less" (returns true if subject is the shorter string).  If both strings are the same length and have the same codes (they are equal) the result is false.

  • The less operator may be combined with the equal operator to perform a "less than or equal to" comparison as shown in the second and third forms of Syntax above.  In these cases, if the subject is less-than or equal-to the comparator then -1 (for true) is returned; otherwise 0 (for false) is returned.  Hopefully it is obvious how this works for numeric values.  For comparison of strings, the pair of PETSCII codes from each corresponding position of the two strings is compared.  The first code pair not equal between the two strings will return the result, which is simply the numeric relation of the two different codes.  If all codes match up to the shorter of the two strings, then the shorter string is considered "less" (returns true if subject is the shorter string).  If both strings are the same length and have the same codes (they are equal) the result is also true.

  • The less operator may be combined with the greater operator to perform a "not equal" (or, if you prefer, "less or more") comparison, as shown in the last two forms of the Syntax above.  In these cases, if the subject is not equal to the comparator then -1 (for true) is returned; otherwise 0 (for false) is returned.  Hopefully it is obvious how this works for numeric values.  For comparison of strings, if the lengths are different, the result is true.  Otherwise the pair of PETSCII codes from each corresponding position of the two strings is compared.  The first code pair not equal between the two strings will return a true result.  Finally, if both strings are the same length and have the same codes (they are equal) the result is false.
It is interesting that BASIC allows some operators to be combined to form new operators (as shown here), but unfortunately it is not very general.  For example you can't combine the not and less operators to form a NOT < operator -- you have to do it manually!  Manual operations are error prone because some people don't know that NOT < is equivallant to greater-than-or-equal-to ( >= ) and is different from strictly more ( > ).  Heck, even people who do know better (like me) will occassionally make mistakes.  Similarly, combining NOT with AND to form NAND is forbidden as well.  Another interesting thing is spaces may appear between the operators when combining them and the order of combination is not important; in programming of other languages (including some other BASICs), this "easy syntax" will fail.
 
It should also be noted that the first form (strictly less than) is generally considered safe with floating-point numbers, while the others are not due the involvement of equality.  Specifically, the nature of floating-pointing numbers means there will often be subtle rounding errors that will make two numbers that should be the same (may even print as the same value) will not be exactly the same leading to an invalid result.  This is true with any finite-representation of real numbers (floating-point in this case) and is not a blemish on CBM.  On the contrary, CBM 8-bit computers use a 32-bit mantissa which has more precision than most 8-bit BASICs or even the "float" type in modern languages such as Visual BASIC and C which use a 24-bit mantissa.
 
Relational operators (like less) are frequently used with IF, DO, and LOOP.
 
 Compare With 
 
 Contrast With 
 
 See Also 

© H2Obsession, 2014, 2019
Thanks to Tiaan Geldenhuys for correction of token value!!