VB.NET Mod: Modulo Division Example - Dot Net Perls
Có thể bạn quan tâm
How can you use modulo division in the VB.NET language? With the Mod operator, you can compute the remainder of a division expression.
Mod is equivalent to the modulo operator in C-like languages. With Mod we can determine whether a number is odd or even (its parity).
Example
This example shows some Mod expressions with constant Integers. When the value 90 goes into 1000 11 times, but leaves a remainder of 10. This is the result of 1000 Mod 90.
And The next Mod expressions show the same principle in action. The Console output is shown.Loop example
Using a Mod expression is appropriate in a For-loop. You can apply Mod to the variable "i". In this program, we display "i" whenever it is divisible by 10.
Detail This style of code is useful in real programs. We can "throttle" an action to occur only occasionally this way.Module Module1 Sub Main() ' Loop through integers. For i As Integer = 0 To 200 - 1 ' Test i with Mod 10. If i Mod 10 = 0 Then Console.WriteLine(i) End If Next End Sub End Module0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190Odd, even numbers
The parity of a number is whether it is odd or even. We can test parity by creating 2 functions in VB.NET that return true or false.
Note IsOdd returns the opposite of IsEven. It correctly handles negative and positive numbers.Note 2 IsEven uses modulo division (with Mod) to see if the number is evenly divisible by 2 (and thus even).Info Both methods return a Boolean (true or false). We test these methods with a simple For-loop.Module Module1 Function IsOdd(ByVal number As Integer) As Boolean ' Handle negative numbers by returning the opposite of IsEven. Return IsEven(number) = False End Function Function IsEven(ByVal number As Integer) As Boolean ' Handles all numbers because it tests for 0 remainder. ' ... This works for negative and positive numbers. Return number Mod 2 = 0 End Function Sub Main() For i = -10 To 10 Console.WriteLine(i.ToString() + " EVEN = " + IsEven(i).ToString()) Console.WriteLine(i.ToString() + " ODD = " + IsOdd(i).ToString()) Next End Sub End Module-10 EVEN = True -10 ODD = False -9 EVEN = False -9 ODD = True -8 EVEN = True -8 ODD = False -7 EVEN = False -7 ODD = True -6 EVEN = True -6 ODD = False -5 EVEN = False -5 ODD = True -4 EVEN = True -4 ODD = False -3 EVEN = False -3 ODD = True -2 EVEN = True -2 ODD = False -1 EVEN = False -1 ODD = True 0 EVEN = True 0 ODD = False 1 EVEN = False 1 ODD = True 2 EVEN = True 2 ODD = False 3 EVEN = False 3 ODD = True 4 EVEN = True 4 ODD = False 5 EVEN = False 5 ODD = True 6 EVEN = True 6 ODD = False 7 EVEN = False 7 ODD = True 8 EVEN = True 8 ODD = False 9 EVEN = False 9 ODD = True 10 EVEN = True 10 ODD = FalseSyntax note
In C-like languages, the "%" character expresses a modulo division. We cannot use this character in VB.NET. Instead the Mod operator is used.
Modulo division is an important concept to understand in computer programming. In .NET, modulo division is used to implement collections such as Dictionary.
The Mod operator often comes in handy whenever a mathematical procedure is needed. We can use modulo for determining parity (odd or even).
Từ khóa » Visual Basic Mod Operator
-
Mod Operator - Visual Basic | Microsoft Docs
-
Mod Operator - Microsoft Docs
-
Can Someone Explain The 'mod' Operator In In The Simplest ...
-
Mod Operator - Microsoft Visual Basic Constants Documentation
-
Mod Operator In Excel VBA (In Easy Steps)
-
Mod - Operator Of Language VBScript - Promotic
-
How To Use MOD In VBA - Excel Champs
-
How To Use VBA Mod Operator (9 Examples) - ExcelDemy
-
Mod Operator - ADM Help Centers
-
Mod Operator - VB .NET Language In A Nutshell [Book]
-
MS Excel: How To Use The MOD Function (VBA) - TechOnTheNet
-
Viacheslav Eremin | Visual Basic Language Reference | Mod Operator