473,385 Members | 1,907 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,385 software developers and data experts.

Relational operator outside loops and conditionals..

Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

..
..
..
state == 2;
..
..

Will the statement be ignored (or as good as ignored) all the times?

Thanks,
Deepak

Aug 9 '06 #1
11 1447
Janus said:
Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

.
.
.
state == 2;
.
.

Will the statement be ignored (or as good as ignored) all the times?
In this case, assuming 'state' is what it appears to be, the compiler is
very likely to ignore it completely, although a good compiler will probably
diagnose it - "code has no effect" or something along those lines.

But if you had:

state == function();

that would be a different matter, since function() may have side effects.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 9 '06 #2


Janus wrote On 08/09/06 12:18,:
Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

.
.
.
state == 2;
.
.

Will the statement be ignored (or as good as ignored) all the times?
The expression is evaluated, and the result of the
evaluation is discarded. The compiler may (or may not)
"notice" that the evaluation makes no difference to the
behavior of the program, and then decide to eliminate it.
If the evaluation has no effect, there is no way for the
program to discover whether it was or was not performed.

There's nothing special about the relational operators
in this regard. Here are some other expressions to ponder:

x + y;
c ? x : y;
x;

.... and, of course

state++ == 2;

--
Er*********@sun.com

Aug 9 '06 #3
Thanks a lot for the reply.
I was using an enum value on Right hand side, and the == was a typo
actually and was not solving my issue. So I was just curious.

Thanks once again,
Deepak
Eric Sosman wrote:
Janus wrote On 08/09/06 12:18,:
Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

.
.
.
state == 2;
.
.

Will the statement be ignored (or as good as ignored) all the times?

The expression is evaluated, and the result of the
evaluation is discarded. The compiler may (or may not)
"notice" that the evaluation makes no difference to the
behavior of the program, and then decide to eliminate it.
If the evaluation has no effect, there is no way for the
program to discover whether it was or was not performed.

There's nothing special about the relational operators
in this regard. Here are some other expressions to ponder:

x + y;
c ? x : y;
x;

... and, of course

state++ == 2;

--
Er*********@sun.com
Aug 9 '06 #4
Janus (in 11**********************@m79g2000cwm.googlegroups. com) said:

| This is a very basic question.
|
| What will happen if there is a statement with a relational operator
| outside while, for, or if conditions?
|
| Will the statement be ignored (or as good as ignored) all the times?

The operator will work as expected. One of my favorite examples:

int sign;
:
sign = (x 0) - (x < 0);

sets the variable sign to 1, 0, or -1 if x is positive, zero, or
negative.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 9 '06 #5
In article <11**********************@m79g2000cwm.googlegroups .com>,
Janus <de****************@gmail.comwrote:
>What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...
.
state == 2;
The important thing to realise - as you seem to have done - is
that == is just an operator, so this statement is not really any
different from

3+4;

Not all languages work like this. In some languages comparisons are
(or were, I don't think many modern languages do it) part of the
syntax of conditionals.

-- Richard
Aug 9 '06 #6
Eric Sosman wrote:
>
Janus wrote On 08/09/06 12:18,:
[...]
state == 2;
[...]
There's nothing special about the relational operators
in this regard. Here are some other expressions to ponder:

x + y;
c ? x : y;
x;

... and, of course

state++ == 2;
I assume that:

state++ == state++;

is still UB, even though there's no assignment?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Aug 10 '06 #7


Kenneth Brody wrote On 08/10/06 11:32,:
>
I assume that:

state++ == state++;

is still UB, even though there's no assignment?
Right: two modifications to the same object without
an intervening sequence point -unbehaved definior.

--
Er*********@sun.com

Aug 10 '06 #8
Kenneth Brody said:

<snip>
I assume that:

state++ == state++;

is still UB, even though there's no assignment?
Yes, it's still UB, because:

"Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be accessed only to
determine the value to be stored."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 10 '06 #9

"Eric Sosman" <Er*********@sun.comwrote in message
news:1155141422.263649@news1nwk...
x + y;
c ? x : y;
x;

... and, of course

state++ == 2;
what about in this case? Is the compiler allowed to not execute the pointer
dereference?

int main(void) {

char *p = some_address();
*p == 3;

return 0;

}
Aug 10 '06 #10


Serve Laurijssen wrote On 08/10/06 14:34,:
"Eric Sosman" <Er*********@sun.comwrote in message
news:1155141422.263649@news1nwk...
>>x + y;
c ? x : y;
x;

... and, of course

state++ == 2;


what about in this case? Is the compiler allowed to not execute the pointer
dereference?

int main(void) {

char *p = some_address();
*p == 3;

return 0;

}
Yes, the compiler can omit the useless comparison.

Argument: If the pointer is valid and the data it points
to is valid and "everything is quite correct," the expression
has no effect. If the pointer is invalid or the object it
points to holds a trap representation or "all occasions do
conspire against me," the effect is undefined. Either way,
the compiler is within its rights to eliminate the expression,
to eliminate p itself, and even to eliminate the call to
some_address() if doing so loses no side-effects.

--
Er*********@sun.com

Aug 10 '06 #11
Serve Laurijssen wrote:
"Eric Sosman" <Er*********@sun.comwrote in message
news:1155141422.263649@news1nwk...
>x + y;
c ? x : y;
x;

... and, of course

state++ == 2;

what about in this case? Is the compiler allowed to not execute the
pointer dereference?

int main(void) {

char *p = some_address();
*p == 3;

return 0;

}
Yes, the compiler is allowed to omit the dereference and comparison
completely. The compiler is also allowed to assume that p points to an
object, at the same time even, and omit any (for example) null pointer
checks it finds afterwards.
Aug 10 '06 #12

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

Similar topics

3
by: Thomas | last post by:
Hi everybody! I have the following code: ---snip--- class BaseRecord { protected: struct tFieldLenData {
15
by: Steven T. Hatton | last post by:
It blows up on a 9 dimensional space, but I'm pretty sure that's due to the size of size_t. --- begin: DataGeneration_Test --- Tensor<T 3, 3>...
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
1
by: Locia | last post by:
I would like compare all type. I try with this function but I get System.NullReference.Exception if pass in Compare function two int type. Why relational operation isn't defined for basic type...
14
by: gupta.keshav | last post by:
Hi, I want to know the trick of implementing condition operator or (if()... else...). Symbols to use: ~, !, ^,&, +, |, <<, >>. NOt to use: if statement, loops,
7
by: Pradeep | last post by:
Hello, I need to take a set of input tables and create an XML output file. The format of the XML output must be user-definable and must be intuitive enough for non-techies to use. input...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
14
by: AliceB.Toklas | last post by:
In my Absolute Beginner's Guide to C book by Greg Perry where it is instruction how relational operators work it gives the following example: int i = 5; so the following statement is true: ...
12
by: ujjc001 | last post by:
Here's one for ya. I want to create a relational operator from a string object, i.e. I want to somehow be able to say: string opString = ">="; int i1 = "20"; int i2 = "10"; if (i1...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.