Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2005, 10:10 PM   #1 (permalink)
arizona99
Registered User
 
Join Date: Nov 2005
Posts: 2
arizona99 is on a distinguished road
This looks so right, but its so wrong? multiple operators in a condition

Code:
int a = 0;
if ( 5 < a < 10 )
   std::cout << "Yep";
It would seem like C++ should evaluate the condition as 5 < a AND a < 10. Why doesn't it work that way? And is there a term for this situation?

-thanks
arizona99 is offline   Reply With Quote
Old 11-03-2005, 11:17 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
The term is improper syntax. What you're really saying is either (true > 10) or (5 < true) depending on how the compiler implements it, neither of which make any sense. The proper syntax would be ( 5 < a && a < 10 ).

EDIT: I hope that doesn't come off as abrasive, so here's a bit more explanation. The < and > operators are not greater than and less than from mathematics, they are comparison operators that compare the value of the expression on the left to the value of the expression on the right and return a boolean true or false.
As such, they cannot be used declaratively like in mathematics because as soon as one is evaluated the comparison no longer has any concept of its input. That's why you need to add a logical operator as glue for the two comparisons, because the logical operator deals with the true or false answers the comparison operators yeild.
__________________
Stop intellectual property from infringing on me

Last edited by teknomage1; 11-04-2005 at 11:23 AM.
teknomage1 is offline   Reply With Quote
Old 12-19-2005, 12:23 PM   #3 (permalink)
arizona99
Registered User
 
Join Date: Nov 2005
Posts: 2
arizona99 is on a distinguished road
its alright. thank you
arizona99 is offline   Reply With Quote
Old 02-19-2006, 02:30 PM   #4 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
Quote:
Originally Posted by arizona99
Code:
int a = 0;
if ( 5 < a < 10 )
   std::cout << "Yep";
It would seem like C++ should evaluate the condition as 5 < a AND a < 10. Why doesn't it work that way? And is there a term for this situation?

-thanks
Specifically, you have to remember that the statements are evaluated individually. Any compound boolean statement has to be broken down into individual statements, which are then evaluated and chained together as appropriate. The statement 5 < a < 10 is the same as ( 5 < a ) < 10.

In this case, 5 < a < 10, the first thing to be evaluated is '5 < a'

This is false. False values are evaluated as 0, while true ones (from boolean operators, anyway), are evaluated as 1. So we swap out '5 < a' for '0'.

This leaves us with '0 < 10', which is true, so the code executes. While tekno's right on about how it should be implemented, it's actually not improper syntax, per se. It's just not syntax that's doing what you want.
kyoryu is offline   Reply With Quote
Old 02-19-2006, 04:27 PM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
You are correct, kyoryu, but trusting the compiler to implement things from left to right is not always a valid assumption. And though booleans can be interpreted as 1 and 0, it's probably not the way you want to think about them most of the time.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 02-21-2006, 11:11 AM   #6 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
Quote:
Originally Posted by teknomage1
You are correct, kyoryu, but trusting the compiler to implement things from left to right is not always a valid assumption.
If you're using a compiler that's even reasonably compliant, it is.

http://www.cppreference.com/operator_precedence.html

It's not a good idea, though, and separating things with parens never hurts.
Confusing code is bad code.

My point was explaining WHY and WHAT was happening. It's worth noting, especially for new programmers, that there's only one ternary operator in c/c++, and that < isn't it.

Quote:
Originally Posted by teknomage
And though booleans can be interpreted as 1 and 0, it's probably not the way you want to think about them most of the time.
No, but knowledge of how C++ actually handles bools, and how it resolves complex statements can help when running into similar problems. You handled the "how do I fix it" bit very well, I just wanted to add the "and this is why it didn't work in the first place" bit
kyoryu is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Order Multiple Items BUFFY PHP 12 10-20-2005 09:14 AM
Multiple Files Lordieth Standard C, C++ 6 09-19-2005 08:13 AM
NumberFormatException: multiple points sde Java 3 07-27-2004 09:44 AM


All times are GMT -8. The time now is 03:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting