MaXuMaS
MaXuMaS

Reputation: 43

How to check if numbers match for if statement vb.net

Will you please give me a gide line on how to solve my current problem. I'm not sure how to put this in practice.

I have a counter which increase by one in a for statement I want to add a if statment that needs to do the following:

Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
   If count = 3 or count = 6 or count = 9 or count = 12 ..and on and on
       'All the numbers that mathes the count
   Else
       'All the numbers that does not match
   End if

   count += 1
Next

I want a simpaler method on how to write the If count = 3 or count = 6 and so on

Upvotes: 2

Views: 1790

Answers (4)

Juffy
Juffy

Reputation: 1220

1) Why do you have i and count which appear to always be the same value?

2) Two possible solutions: either the Mod operator as others have noted, assuming you actually want every third number, or:

For i As Integer = 1 To 400 - 1
   Select Case i
       Case 3,6,9,12,15....
           'Do stuff here for matching
       Case Else
           'All the numbers that does not match
   End Select
Next

Upvotes: 2

Heslacher
Heslacher

Reputation: 2167

If the count should be dividable by 3 without a rest (as it seems to be the case), you can use the Mod operator: Documentation

The Mod operator will divide 2 numbers and will return the remaining, so e.g 14 Mod 3 will be 2. So the only check, you need to do, is if count Mod 3 = 0 like:

Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
   If count Mod 3 = 0 then
       'All the numbers that mathes the count
   Else
       'All the numbers that does not match
   End if

   count += 1
Next

Upvotes: 2

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

I'm not sure about syntax, but you need to use Mod operator:

Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
   If (count Mod 3) = 0
       'All the numbers that mathes the count
   Else
       'All the numbers that does not match
   End if

   count += 1
Next

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44834

Modulus is your friend.

number1 Mod number2


if count MOD 3 = 0

http://msdn.microsoft.com/en-us/library/se0w9esz(v=vs.90).aspx

Upvotes: 1

Related Questions