Visual Basic

What Does Mod Do In Visual Basic

In Visual Basic, Mod returns the remainder after one number is divided by another. For example, 10 Mod 3 returns 1 because three fits into ten three times, leaving one. Use Mod to test divisibility, identify even or odd integers, and make counters repeat within.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.

How to write a Mod expression

The syntax is result = dividend Mod divisor. The dividend is the number being divided; the divisor is the number you divide it by. The result stores what remains.

For example, Dim remainder As Integer = 17 Mod 5 stores 2 in remainder: five fits into seventeen three times, accounting for fifteen, with two left over.

Mod is an operator, so you place it between its operands. Write 17 Mod 5 in Visual Basic; the % remainder syntax used in languages such as C# does not belong in that expression.

The examples here use Visual Basic for .NET, often called VB.NET. If you are learning the language, the Visual Basic guides provide related explanations of syntax and program structure.

Choose between Mod, integer division and ordinary division

These operators answer different questions. Choose the result you need before writing the expression, rather than calculating a quotient and trying to extract the remainder afterwards. Microsoft's arithmetic operators guide explains the distinction.

What you need Expression Result
The remainder 17 Mod 5 2
The whole-number quotient 17 \ 5 3
The quotient including its fractional part 17 / 5 3.4

For instance, when packing seventeen items into groups of five, integer division tells you there are three complete groups. Mod tells you that two items remain unpacked.

Check divisibility and even or odd numbers

For integers, a remainder of zero means the dividend is exactly divisible by the non-zero divisor. The expression number Mod 4 = 0 therefore tests whether number is a multiple of four.

To use that test in a conditional statement:

  1. Choose the divisor, such as 4.
  2. Calculate number Mod 4.
  3. Compare the result with zero in an If condition.
  4. Run the appropriate branch depending on whether the comparison is True or False.

For example, If number Mod 4 = 0 Then begins the branch for multiples of four. Complete the statement with the actions to perform and End If.

For even numbers, test number Mod 2 = 0. For odd numbers, test number Mod 2 <> 0. The latter also works for negative odd integers, whose remainder may be -1 rather than 1.

If several parts of your program need the same check, put the surrounding actions in a reusable procedure. The guide to subroutines in Visual Basic explains how to organise repeated work.

Make an index repeat through a fixed range

Mod is useful when a counter should return to the beginning after reaching the end. For three options indexed from zero, the valid positions are 0, 1 and 2.

Use currentIndex = (currentIndex + 1) Mod 3 to advance through those positions. Starting at 0, successive updates produce 1, 2, 0, 1 and so on.

If your labels run from 1 to 3 instead, use currentIndex = (currentIndex Mod 3) + 1. Keep the indexing convention consistent: a formula for zero-based positions produces a different range from one for one-based labels.

In a game, a repeating counter could rotate through players or alternate turns. You can practise that idea alongside the broader game-building steps in how to make Hangman in Visual Basic.

Handle negative values and fractional operands correctly

A negative dividend can produce a negative remainder

In VB.NET, a non-zero remainder has the same sign as the dividend. For example, -10 Mod 3 returns -1, while 10 Mod -3 returns 1. Microsoft's Mod operator reference documents this behaviour.

This matters when wrapping an index backwards. If currentIndex is 0, (currentIndex - 1) Mod 3 produces -1, which is outside the intended range. For positions already restricted to 0, 1 and 2, use (currentIndex + 2) Mod 3 to move back one position.

VB.NET and VBA treat fractional operands differently

VB.NET supports fractional remainders: 12.5 Mod 5 returns 2.5. Single and Double values can introduce binary floating-point precision effects, so a fractional divisibility check may not behave like an integer check.

For exact decimal quantities, consider Decimal operands. Merely storing a Double calculation in a Decimal variable does not remove an error already introduced during the calculation; Microsoft's data type troubleshooting guide illustrates this distinction.

VBA, used in Office macros, rounds fractional operands to integers before applying Mod. Do not transfer a fractional VB.NET example into a macro and expect the same result. See Microsoft's VBA Mod reference for that behaviour.

Check the divisor and expression grouping

Validate a divisor supplied by the user before calculating a remainder. In VB.NET, integer and Decimal remainder operations with a zero divisor fail; Single and Double remainder operations with a zero divisor yield NaN, meaning “not a number”. These cases are covered by the Visual Basic language specification.

Mod is evaluated after multiplication, ordinary division and integer division, but before addition and subtraction. Use parentheses to make your intent clear: (currentIndex + 1) Mod 3 groups the increment before calculating the remainder. Microsoft's operator precedence reference lists the full order.

Try Console.WriteLine(number Mod 2) in a small VB.NET console project with number set to 8, 9 and -9 in turn. Then change the expression to number Mod 2 <> 0 and check that it identifies both positive and negative odd integers.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post