Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Question about the conditional operator (http://www.programmingforums.org/showthread.php?t=13702)

357mag Aug 5th, 2007 4:14 AM

Question about the conditional operator
 
Look at this code snippet:

:

for (int i = -5; i < 6; i++)
{
    result = i != 0 ? 50 / i : 0;

      if (i != 0)
            Console.WriteLine("50 / " + i + " is " + result);

      if (i == 0)
            Console.WriteLine("\ni is equal to " + i + " so division not allowed.\n");


Here we use the conditional operator to prevent a division by zero from taking place. If i is not equal to 0, the compiler takes 50 and divides it by the value of i and assigns the result to result. When the test expression i != 0 fails it assigns 0 to result.

Okay. Basically I understand how the conditional operator works. It's similar to an if-else statement. But here is what I'm not sure about. According to my precedence charts, the division operator(/) has a higher precedence than either the not equal to operator(!=) or the conditional operator itself(?:).

So if division has the highest priority of all the operators in this example, why does the code succeed without having to use parentheses? Is it because I'm kinda looking at it wrong? I mean the way you have to look at it is the not equal to operator(!=) has a higher precedence than the conditional operator, and the conditional operator in this example is kind of enclosing the division operation so the division operation doesn't really stand out like it normally might. I don't know if you understand what I mean but the code obviously functions fine without any parentheses so that's what I gather.

lectricpharaoh Aug 5th, 2007 10:00 AM

I think you're overthinking it. The conditional operator evaluates to much the same code (perhaps even identical) to an if..else block. Say you have the following:
:

result = A ? B : C;
Now, regardless of precedence, neither B nor C can be evaluated until A has been evaluated, as it is A that controls which of B or C is evaluated; it is guaranteed that one (and only one) of B and C will be.

DaWei Aug 5th, 2007 12:13 PM

Perhaps you could just read the docs concerning the ternary operator:
Quote:

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:

The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.

If the first operand evaluates to true (1), the second operand is evaluated.

If the first operand evaluates to false (0), the third operand is evaluated.
In your example, if i == 0 the 50/i will never be evaluated.

The Dark Aug 5th, 2007 10:46 PM

The higher precedence means the way the operations are grouped together, not necessarily the order that they are processed. You can work this out by adding brackets in the same order that the compiler treats the operations.
From your example:
:

result = i != 0 ? 50 / i : 0;
The highest precedence is the division, so this means the equation looks like:
:

result = i != 0 ? (50 / i) : 0;
Next highest is the != which gives us
:

result = (i != 0) ? (50 / i) : 0;
Next is the conditional, which gives:
:

result = ((i != 0) ? (50 / i) : 0);

357mag Aug 6th, 2007 12:25 AM

Interesting. The books usually say "order of operations" meaning that if there is a division or multiplication present, those are carried out first. That's why I get confused. I look at the 50 / i and think to myself, "the compiler sees that first" but like that one fellow said, I think I'm over-doing it. After all, this is basically an if-else statement which means the compiler must be looking evaluating the first part first, otherwise the whole deal would be meaningless.


All times are GMT -5. The time now is 2:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC