VB.NET If statement is inconsistent with standard?

  • Thread starter Thread starter Larry Woods
  • Start date Start date
L

Larry Woods

Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the "If"
statement since the division operation will cause a "divide by zero"
exception. But this doesn't happen. The following statement, added before
the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or, better
yet, what am I missing?

TIA,

Larry Woods
 
Larry Woods said:
Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the
"If" statement since the division operation will cause a "divide by
zero" exception. But this doesn't happen. The following statement,
added before the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or,
better yet, what am I missing?

Floating point divisions by zero don't result in an exception. The Result is
"infinity" and can be stored in a floating point variable.

Integer divisions do throw an exception.

j / i is a floating point division, because "/" is used (instead of "\") for
integer divisions. Therefore, j and i are converted to floats before
division and afterwards the result (infinity) is assigned to j. The
assignment leads to an exception because infinty can not be assigned to an
Integer variable.

The expression used with the If statement does not result in an exception,
becasue it doesn't have to be assigned to an Integer variable.
 
Larry,
VB.NET If statement is inconsistent with standard?
There is nothing wrong with the If statement, its the expressions that you
are trying to evaluate!

The regular division operator "/" does not cause a Divide by Zero Exception!
It returns a value based on the IEEE 754 arithmetic. In other words it
returns Nan, Positive Infinity or Negative Infinity.

You need to use the integer division operator "\" to get a division by zero
exception.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec11_5_4.asp

If your example, the regular division operator returns Infinite. Infinity
does not equal 7, so it checks if i = 0, which it does.
the "If" WILL cause an exception...natually:
j=j/i

This causes an Overflow Exception, as Infinity will not fit in a 32 bit
integer value. It does not cause a Divide By Zero exception!

Check out the Nan, PositiveInfinity, and NegativeInfinity fields of the
Single & Double classes. Also check out the IsNan, IsInfinity,
IsPositiveInfinity, and IsNegativeInfinity methods of the Single & Double
classes.

Hope this helps
Jay
 
It is because the result of 1/0 is infinity and that does not equal 7.

MessageBox.Show(j / i) displays Infinity


--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the "If"
statement since the division operation will cause a "divide by zero"
exception. But this doesn't happen. The following statement, added before
the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or, better
yet, what am I missing?

First, you should always have:

Option Strict On
Option Explicit On

This will help you catch small type conversions that can happen in the
background without you knowing it. In the second case (j=j/i), without
Option Strict On, VB is doing a conversion behind-the-scenes and causing
an overflow exception.

Check the VB.NET docs for the division operator:

ms-help://MS.NETFrameworkSDK/vbls7/html/vblrfVBSpec11_5_4.htm

There's two different ones. The normal one ("/") and the integer
division operator ("\"). You're using integers with the normal one, so
VB.NET converts j and i to doubles and does the division. According to
the specs: "The quotient is computed according to the rules of IEEE 754
arithmetic".

After defining i and j try adding:

Dim k as double = i / j

And check out the value of k in the watch window.
 
Larry,
Interesting, I have not your answer I did some test, they seem to explain
something to me but like you I am curious about the deeper rules behind
this.
\\\\\
Dim i As Integer = 0
Dim j As Integer = 5
If (j / i) = System.Double.PositiveInfinity Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If
////
Results in "In if", it looks if it is placed in a double before the
evaluating.
I am curious too where to find this in the documentation.
I hope we get a lot of answers, maybe we are only dummies.
Cor
 
Cor,
You can find the info spread across, the Operators Section & the Conversions
section of the Visual Basic .NET Language specifications.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec11.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec11_5.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec10.asp

Not sure if there is a 'easier to read' web page available or not.

Hope this helps
Jay
 
Jay,
I am making a message to Patrick.
I write in that that I am a dummy.
I don't know if in English is to that proverb about salt and wounds
:-)
Cor
 
because in if statement you are checking a value for true
or false.the statement is wrong so it evaluates the 'OR'
Condition.Division operation will not take place.

when you add j=j/i you are assigining the resulting value
of j/i to j. in this case exception is thrown.
hope my explanation is right.any comments?
 
Hi, you're slightly wrong.

The If statement is evaluated, but it's not wrong. A floating point division
is performed and anything divided by Zero is undefined. Undefined values are
allowed in floating point variables, and the / operator is a floating point
division. The result is not being assigned, so no exception is thrown.

The resulting value of the floating point division is Undefined, but
integers can not be undefined. So when trying to assign Undefined to an
integer, the exception is thrown.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Back
Top