Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

= (equal)

IFKeyword Abbreviation Token (hex) Version(s) Classification
= none B2 1.0 to 7.0 Operator (4)

  Syntax  
subject comparator
 ~ or ~
subject < comparator
 ~ or ~
subject = comparator
 ~ or ~
subject > comparator
 ~ or ~
subject comparator
 
Parameters Type Legal Value(s) Default Value Note(s)
subject Numeric or String
any
comparator Numeric or String any
Type must match subject (numeric or string)
 
Returns Type Value(s) Note(s)
result Boolean
-1 (true) or 0 (false)
 
  Purpose  
Evaluate a comparative relation.

 
  Remarks  
The equal operator returns a boolean result; it may be used in one of three ways:
  • In the first Syntax shown above, known as "equal to ", 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.

  • The equal operator may be combined with the less 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 equal operator may be combined with the more operator to perform a "greater than or equal to" comparison, as shown in the last two forms of the Syntax above.  In these cases, if the subject is greater-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 longer string is considered "more" (returns true if subject is the longer string).  If both strings are the same length and have the same codes (they are equal) the result is also true.
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 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 many languages (including some BASICs), this "easy syntax" will fail.
 
It should also be noted that all forms are generally considered unsafe with floating-point numbers due to 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 happens 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 is 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.
 
Finally, another use of the equal token, totally unrelated to relational comparison, is with the assignment of values to variables.  See LET and MID$.
 
For the opposite case (inequality) see either the less or more operators (< or >).
 
Relational operators (like equal) are frequently used with IF, DO, and LOOP.
  
  Contrast With  
<, >, LET, MID$
 
  See Also  

© H2Obsession, 2014
Comments