Evaluating Expressions - Visual Basic

Evaluating Expressions - Visual Basic


Most programmers probably already know this, but I have observed that for beginner programmers, it is quite frequently unknown or overlooked.

When you evaluate expressions you need not evaluate "If <expression> = true then", you simply evaluate "If <expression> then"

This is because <expression> already produces a Boolean value, and an if then branch breaks down to the following logic:

1.If <Expression> then
2.    'True was the case
3.Else
4.    'False was the case
5.End If

So when you evaluate the following expression for example:

(1+2 = 4)

This will automatically become false, because 1+2 does not equal 4.

True will always = true (It is what it is!)
False will always = false(It is what it is!)

Which means that saying:

1.If (1+2=4) = true then
2.    'True was the case
3.Else
4.    'False was the case
5.End If

is effectively saying the same as saying

1.If <Boolean> = true then
2.2.    'True was the case
3.3.Else
4.4.    'False was the case
5.5.End If


Bus since a Boolean "is what is", we need not check if "<Boolean>" = true...
So the following is the same as above:

1.If <Boolean> then
2.    'True was the case
3.Else
4.    'False was the case
5.End If

And the following is just as absurd as evaluating true in an expression:

1.If (((((1 + 2) = 3) = True) = True) = True) Then
2.    'True was the case
3.Else
4.    'False was the case
5.End If

In conclusion, try to remember, "It is what it is."

References

Sort by: Published Date | Most Recent | Most Useful
Comments
  • Hi Paul, I think it will be nice if you add more into this article about short-cutting and various ways of writing if and logic in VB.NET (I think there is a syntax, but I always forgot exact details). If you also add a bit of the similar for C# (e.g. how to short cut IF logic) then it will be great. It may be not the exact topic of this article, but something I often need and when I need it, I do an extra research every time

Page 1 of 1 (1 items)