Mod (operator)


Expression1 Mod expression2


Returns the remainder of expression1 / expression2 as a whole number.


If both expressions are integers, then the result is an integer. Otherwise, each expression is converted to a Long before performing the operation, returning a Long.

A runtime error occurs if the result overflows the range of a Long.

If either expression is Null, then Null is returned. Empty is treated as 0.


This example uses the Mod operator to determine the value of a randomly selected card where card 1 is the ace (1) of clubs and card 52 is the king (13) of spades. Since the values recur in a sequence of 13 cards within 4 suits, we can use the Mod function to determine the value of any given card number.

Const crlf = Chr$(13) + Chr$(10)

Sub Main()
  cval$ ="Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King"
  card% = Random(1,52)
  value = card% Mod 13
  If value = 0 Then value = 13
  CardNum$ = Item$(cval,value)
  If card% < 53 Then suit$ = "Spades" 
  If card% < 40 Then suit$ = "Hearts"
  If card% < 27 Then suit$ = "Diamonds"
  If card% < 14 Then suit$ = "Clubs"
  msg1 = "Card number " & card% & " is the "
  msg1 = msg 1& CardNum & " of " & suit$
  MsgBox msg1
End Sub

See Also

/ (operator); \ (operator).

More information