By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Relational operator outside loops and conditionals..

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a


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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a

"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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.