473,503 Members | 4,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where are these odd comparison usages useful?

I read on a blog where someone was baffled by the outcome of code such
as:

if (a == b == 0) { /* if a AND b == 0, execute */ }
if (a && b == 0) { /* if a AND b == 0, execute */ }

It just depends on how you evaluate this code as to what the outcome is
-- for example the author's comments do not reflect an understanding of
this at all. But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior to
me.

Can anyone give me an example where code like this is beneficial?

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Dec 9 '07 #1
3 1100
On Dec 8, 10:00 pm, "-Lost" <maventheextrawo...@techie.comwrote:
I read on a blog where someone was baffled by the outcome of code such
as:

if (a == b == 0) { /* if a AND b == 0, execute */ }
if (a && b == 0) { /* if a AND b == 0, execute */ }
Those /**/ comments are not great.

var a = 0;
var b = 0;

if (a == b == 0) {
alert('first'); // doesn't execute
}

if (a && b == 0) {
alert('second'); // doesn't execute
}

var a = 1;
var b = 0;

if (a == b == 0) {
alert('first'); // does execute
}

if (a && b == 0) {
alert('second'); // does execute
}

It just depends on how you evaluate this code as to what the outcome is
You don't choose how to evaluate the code. Hopefully, in any
particular host, the ECMAScript standard has specified the outcome.

-- for example the author's comments do not reflect an understanding of
this at all. But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior to
me.

Can anyone give me an example where code like this is beneficial?
I would say the first code is a bad practice. The second code could be
written perhaps more clearly as the following.

if (a && (b == 0)) {/* ... */}

--
Peter
Code Worth Recommending Project
http://cljs.michaux.ca

Dec 9 '07 #2
AKS
On 9 ÄÅË, 11:00, "-Lost" <maventheextrawo...@techie.comwrote:
>But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior >to me.
I was writing in the same way until I'd been explained (and I got it
right) that it 's too bad.
So the reason is an absence of experience
Dec 9 '07 #3
Response to Peter Michaux <pe**********@gmail.com>:
>It just depends on how you evaluate this code as to what the
outcome is

You don't choose how to evaluate the code. Hopefully, in any
particular host, the ECMAScript standard has specified the
outcome.
That's my inexperience showing in the way in which I word things.

I meant the outcome was dependent on the values of "a" and "b."

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Dec 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
5641
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
47
3575
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
5
2526
by: Mark | last post by:
Hi - I have set-up security for my users - the security is held in a text field, separated by a comma. If the users a member of groups 1, 5 and 6 - the usergroups field is set to 1,5,6 - to...
17
5003
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
10
7551
by: joshsackett | last post by:
I am starting an encryption project for my database and I'm performing some tests on decryption speed. A lot of my application queries use a LIKE parameter in the WHERE clause. To keep from...
29
2589
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
37
2761
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
3
5518
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips...
16
3642
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.