Results 1 to 10 of 10

Thread: How would I write "If x cannot be evenly divided by 3, and x is not 0 then..."

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    How would I write "If x cannot be evenly divided by 3, and x is not 0 then..."

    I know how to write if x cannot be divided evenly by 3

    [code]
    If x Mod 3 = 0 Then
    x = x + 1
    End If
    [code]

    but how would i write the and x is not equal to 0?

  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    Code:
    If x Mod 3 = 0 And Not x = 0 Then

  3. #3
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    If x Mod 3 = 0 And x <> 0 Then
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    OOPS Basic misunderstasnding here...

    I know how to write if x cannot be divided evenly by 3

    Code:
    If x Mod 3 = 0 Then
      x = x + 1
    End If
    x Mod 3 = 0 if AND ONLY if x CAN be divided equally by 3 (i.e. x = 3, 6, -3, 0)

    Note that if x = 0 then x Mod 3 = 0

    SO:

    If x Mod 3 <> 0 Then x cannot be divided equally by 3 AND x cannot be 0.

    So just use

    Code:
    If x Mod 3 <> 0 Then...
    Cheers,

    P.


    Not nearly so tired now...

    Haven't been around much so be gentle...

  5. #5
    Registered User Lior's Avatar
    Join Date
    Jan 2000
    Posts
    307

    Cool Final Answer:

    Final piece of code:

    Code:
    Dim X
    If (X Mod 3)<>0 And X<>0 Then
    '
    '
    End If

  6. #6
    Guest
    can som1 explain to me what MOD is/does? i looked it up and i can see how it works but i cant see how you peeps code will work


    at the moment for things like this i would use

    If (x / 3) = Int(x / 3) And x <> 0 then


    End If

  7. #7
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    To re-iterate. 0 Mod n = 0 for any value of n
    THEREFORE: If X Mod 3 <> 0 Then X CANNOT BE ZERO! That means that the 'And X <> 0' is redundant.

    chenko: Mod is an operator that returns the remainder once the value has been divided by the divisor. So X Mod n will divide X by n and return the value of any remainder, thus 0 < X Mod n < n-1

    0 Mod 3 = 0
    1 Mod 3 = 1
    2 Mod 3 = 2
    3 Mod 3 = 0
    4 Mod 3 = 1
    5 Mod 3 = 2
    6 Mod 3 = 0
    7 Mod 3 = 1
    8 Mod 3 = 2
    etc.

    OK?

    P.


    Not nearly so tired now...

    Haven't been around much so be gentle...

  8. #8
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    base conversion

    FYI:

    You can use the mod operator for base conversion from a
    higher base to a lower base. Like paul showed you, mod3
    always returns 0,1,2 the digits of base 3. Base2 conversions
    are useful:
    Mod 2 always returns 0 and 1 so to convert 9 to base2:

    9 Mod 2 = 1 - this is the first digit from right to left.
    then 9 / 2 = 4
    then 4 Mod 2 = 0 - next digit R -> L
    then 4 / 2 = 2
    then 2 Mod 2 = 0 - next digit
    then 2 / 2 = 1
    then 1 Mod 2 = 1 - next digit
    then 1 / 2 = 0
    STOP

    so your binary number is: 1001

    to check: 1*2^0 + 0*2^1 + 0*2^2 + 1*2^3 = 9

    I can send you the code for a base10 to base2 converter if
    you are interested.

  9. #9
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Nice!

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  10. #10
    Guest
    oh the remainder... i looked and got confused with th other part...thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width