Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 5th, 2007, 3:14 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.

Last edited by 357mag; Aug 5th, 2007 at 3:15 AM. Reason: spelling error
357mag is offline   Reply With Quote
Old Aug 5th, 2007, 9:00 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Aug 5th, 2007, 11:13 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 5th, 2007, 9:46 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
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);
The Dark is offline   Reply With Quote
Old Aug 5th, 2007, 11:25 PM   #5
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
operator() question???? pegasus001 C++ 4 Mar 13th, 2007 12:18 PM
Bitwise Operator Question grimpirate PHP 1 Nov 13th, 2006 2:39 AM
friends, templates, and other s**t bl00dninja C++ 4 Oct 14th, 2006 1:15 AM
operator overloading question.. sackarias C++ 8 Mar 9th, 2006 3:30 PM
Operator Overloading issue [solved] Dizzutch C++ 4 Jan 29th, 2005 7:46 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:04 AM.

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