in

ASP.NET Weblogs

MindFill - Brian Carroll's Blog

Do While (WhatYouKnow) < (WhatYouKnow + 1)

Parentheses and Order of Operations

It's a good idea to be explicit with parentheses when building arithmetic expressions.  For example, at first the following code doesn't look like it would cause a problem:

Dim i As Integer = 1
i = i + i - i
Console.Writeline(i)

Executing this code will return 1 as you should expect.  But consider what happens in this case:

Dim i As Integer = i.MaxValue
i = i + i - i
Console.Writeline(i)

This code ends with an overflow error which could have been prevented by simply using parentheses like this:

i = i + (i - i)

Comments

 

NICKII said:

Heyy i'm Nickii....I love to dance but this site does need some more renovations to it. Oh and I love my friend Jenn........she's is the smartest, greatest, popular girl i've ever met. I LOVE HEESUNG

October 1, 2007 8:19 PM

Leave a Comment

(required)  
(optional)
(required)  
Add