473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can you do if ( x < y < z ) fctn1() ; ?

Is that valid C or C++ syntax?
Or must one do x<y && y<z ?
Jun 27 '08 #1
6 13743
On Apr 22, 3:24 pm, Robert <irishhac...@gmail.comwrote:
Is that valid C or C++ syntax?
Or must one do x<y && y<z ?

The latter. If your assumed intention is correct.
Jun 27 '08 #2
"Robert" <ir*********@gmail.comwrote in message
Is that valid C or C++ syntax?
Depends on the types of x, y and z. For integers (that you are probably
assuming) it would compile.
Or must one do x<y && y<z ?
Realize that x<y returns a bool, i.e. true or false. You then compare it
against whatever type "z" has. So the intention of "x<y<z" looks wrong.

--
http://techytalk.googlepages.com
Jun 27 '08 #3
On 2008-04-22 16:24:14 -0400, Robert <ir*********@gmail.comsaid:
Is that valid C or C++ syntax?
Yes, both.
Or must one do x<y && y<z ?
They do two different things. People generally want the latter.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4
Robert wrote:
Is that valid C or C++ syntax?
Or must one do x<y && y<z ?
It actually is valid syntax, but it doesn't do what you want. The
first comparison of "x < y < z" produces a bool result (true or false)
that is then used in the second comparison. Not good.

Stay with the second alternative!
Bo Persson
Jun 27 '08 #5
On Apr 22, 10:24 pm, Robert <irishhac...@gmail.comwrote:
Is that valid C or C++ syntax?
Or must one do x<y && y<z ?
Please do not write the main body of the article into the article
title.

As syntax goes the following is correct: if ( x < y < z ) fctn1() ;

however, it may not produce what you expect.

If x, y and z are int values, then x < y evaluates to a bool, is
converted to 0 for false, 1 for true in integral promotion, then
compared to z.

Try the following code. Your compiler should produce appropriate
warnings.

#include <iomanip>
#include <iostream>

int main()
{
std::cout << std::boolalpha << static_cast<bool>( 3 < 2 < 1 ) <<
std::endl;
std::cin.get();
}
Jun 27 '08 #6
Robert <ir*********@gmail.comwrites:
Is that valid C or C++ syntax?
Or must one do x<y && y<z ?
Despite the negative overlook of the other answers, notice that:

a b a<b (b ==a) not(b ==a)
0 < 0 false true false
0 < 1 true false true
1 < 0 false true false
1 < 1 false true false

a<b <=not(b ==a)

so:

bool p=x<y<z;

stores in p: not(z ==x<y)
which may or may not be useful, depending on your problem.

That said, I would define keywords to make it clearer:

#define notimpliedby <
#define impliedby >=
#define implies <=
#define doesnotimply >

so you can write. using parentheses:

int precipitation;
int sunshine;
int temperature_celcius;
if(((precipitation<10)implies(8<sunshine))and(temp erature_celcius>20)){
cout<<"Good weather";
}else{
cout<<"Let's go south";
}

--
__Pascal Bourguignon__
Jun 27 '08 #7

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

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
1
by: Kashish | last post by:
Is file<<"Some string"<<endl is an atomic operation. where file is an ofstream object. If we output a string in ofstream and we provide endl after that,Can we make sure the whole string will be...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
4
by: spibou | last post by:
I saw it at http://www.faqs.org/rfcs/rfc1305.html Is it not the same as writing ``if (m)'' ?
2
by: sf | last post by:
Hundsome Money Online Guaranteed payment month after month · Work part time or full time as you like . Make quick good cash working 2 to 3 hours a day . Work anywhere from home/ office without...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.