Explanation of ? operator missing in reference guide

S

Skybuck Flying

Not only is it not documented in the reference guide but it's syntax/use is
counter-intuitive:

http://www.eskimo.com/~scs/cclass/int/sx4eb.html

One would expect false to be represent by 0 and true to be represented by 1

Therefore

A ? B : C

The logical order would be:

A ? 0 : 1

But instead it's

A ? 1 : 0

One could argue that this resembles the if-else statement, though the
if-else statement is usually on seperate lines while the question mark is
represented to be on a single line and therefore a completely different
language element thus special treatment is warranted.

I find

A ? 0 : 1 more logical in this reguard when seeing it on a single line.

Ofcourse this argumenting is useless.... since it's already part of the
""""""""""standard""""""""""

Just taking a piss at the language itself, may I ? ;)

Bye,
Skybuck.
 
L

Lew Pitcher

Not only is it not documented in the reference guide but it's syntax/use
is counter-intuitive:

http://www.eskimo.com/~scs/cclass/int/sx4eb.html

One would expect false to be represent by 0 and true to be represented by
1

Close enough. A non-zero value is, when evaluated as a condition, taken as
a "TRUE" value, while a zero value is, when evaluated as a condition, taken
as a "FALSE" value.

Your expectations are close enough.
Therefore

A ? B : C

The logical order would be:

A ? 0 : 1

I don't see how you got that. The trinary operator, by definition (and by
analogy to the "if" statement) puts the "true" actions ahead of the "false"
actions.

Either your example is irrelevant (the "true" and "false" actions, after
all, are arbitrary as far as the language is concerned, and only
have "meaning" in the context of the logic in which the trinary operator is
used. In other words, if it is your intent as a programmer to present an
rvalue of 0 when a certain condition is true, and 1 when it is not, then
the above example is correct, otherwise, it is not), or your example is
contrary to the language.

You seem to be saying that (in pseudocode)
IF condition is TRUE
THEN
result is FALSE
OTHERWISE
result is TRUE
FI
is the "logical order" of things, or perhaps that (again in pseudocode)
IF condition is FALSE
THEN
result is FALSE
OTHERWISE
result is TRUE
FI
is the logical order. But, the trinary operator neither orders the false
condition first nor does it evaluate the falseness of the condition.

But instead it's

A ? 1 : 0

If you mean something like
B = A ? 1 : 0;
is like
B = (A != 0)
then, you are correct.
One could argue that this resembles the if-else statement, though the
if-else statement is usually on seperate lines while the question mark is
represented to be on a single line and therefore a completely different
language element thus special treatment is warranted.

Nonsense!!

B = A ? /* test truthfulness of A */
1 : /* A is TRUE - result is 1 */
0 ; /* A not TRUE - result is 0 */

See? Three lines. Just the same as
if (A)
B = 1;
else B = 0;

I find

A ? 0 : 1 more logical in this reguard when seeing it on a single line.

/You/ may find the contra-expression "more logical", but most others don't.
Your failings are none of our business. Why do you feel the need to admit
them to us?

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

Skybuck Flying

It's very easy to explain the logic behind my reasoning:

1 comes after 0.

So

A ? 0 : 1

Is the most logical way to view things ;)

Now imagine if it was a case statement operator:

A ? 0 : 1 : 2 : 4 : 5

^ Highly logical.

I rest my case ;)

Bye,
Skybuck.
 
S

Skybuck Flying

Joe Wright said:
You don't need permission, of course. Your argument seems nonsense.

int A, B;
A = 1; /* true */
B = A ? 1 : 0; /* B follows A */

The above is perfectly logical. Same as..

if (A)
B = 1;
else
B = 0;

Another way to view it is:

A ? initializer : action.

if A is not initialized then initialize.
if A is initialized then take action.

Thus the false would be first, the true would be second.

Bye,
Skybuck.
 
B

Bartc

Skybuck Flying said:
It's very easy to explain the logic behind my reasoning:

1 comes after 0.

So

A ? 0 : 1

Is the most logical way to view things ;)

Now imagine if it was a case statement operator:

A ? 0 : 1 : 2 : 4 : 5

You need C's :? operator which does exactly what you want:

A : 0 ? 1
 
R

Richard Tobin

Skybuck Flying said:
Now imagine if it was a case statement operator:

A ? 0 : 1 : 2 : 4 : 5

Algol 68 has an abbreviated case statement similar to that:

(A | 0, 1, 2, 4, 5 | -1)

(-1 is the default case). But in Algol 68 it's just an abbreviation;
the case "statement" is an expression anyway.

-- Richard
 
B

Bartc

Richard Tobin said:
Algol 68 has an abbreviated case statement similar to that:

(A | 0, 1, 2, 4, 5 | -1)

(-1 is the default case). But in Algol 68 it's just an abbreviation;
the case "statement" is an expression anyway.

Skybuck is assuming A is going to be 0 or 1. In practice the first argument
to ?: could be any integer value, so ?: is not useable as an indexed 2-way
selection, without doing something like !A ? B : C, which looks confusing.

The A-68 construction would be quite a neat addition to C, although A-68 I
think uses 1-based indexing.
 
L

luserXtrog

It's very easy to explain the logic behind my reasoning:

1 comes after 0.

This is only true in the universe of natural numbers. It is not true
in our universe based on the evidences of palaeography, cosmology,
theology, and love. One must have something before one can know the
lack of it.
So

A ? 0 : 1

Is the most logical way to view things ;)

....unless you relate it to the question mark as it behaves in natural
languages, which it greatly resembles in shape and encoding. N'est-ce
pas? Oui : Non; .
Now imagine if it was a case statement operator:

A ? 0 : 1 : 2 : 4 : 5

^ Highly logical.

I rest my case ;)

Begging the question.

Assume proposition 1 is true.
The implication "proposition 1 implies proposition 1" is true.
The conclusion "proposition 1 is true" therefore follows.

So what?!
 
S

Skybuck Flying

Skybuck Flying said:
It's very easy to explain the logic behind my reasoning:

1 comes after 0.

So

A ? 0 : 1

Is the most logical way to view things ;)

Now imagine if it was a case statement operator:

A ? 0 : 1 : 2 : 4 : 5

^ Highly logical.

I rest my case ;)

Ohoh, I see I made a mistake, I ment to write:

A ? 0 : 1 : 2 : 3 : 4 : 5

Like a nice incrementing case statement... oh well ;) :)

Bye,
Skybuck.
 
S

Skybuck Flying

Bartc said:
You need C's :? operator which does exactly what you want:

A : 0 ? 1

1. No documentation.
2. Does not compile.

Please provide example within 3 days eitherwise you end up on my ban list.

Bye,
Skybuck.
 
L

luserXtrog

Ohoh, I see I made a mistake, I ment to write:

A ? 0 : 1 : 2 : 3 : 4 : 5

Like a nice incrementing case statement... oh well ;) :)

While perhaps aesthetically balanced upon the line, one wonders of
what possible use such a construct would be. It appears that,
dependant upon the value within the variable A (presuming an int), the
expression returns an integer value coincidentally indentical to the
value retrieved from A itself. There is such a an expression already:

A;

If, on the otherhand, the illustration was intended to illustrate
resolving an integer to a different associated value, one would
normally use an array. C99 allows complex literals permitting
something like this:

#include <stdio.h>
int main(void) {
int A=0;
printf("%d\n", (int []){1,2,3,4,5}[A]);
/* or even */
printf("%d\n", A[(int []){1,2,3,4,5}]);
}

When A is 0, the expression A[(int []){1,2,3,4,5}] resolves to the
first element of the anonymous array, here 1.
 
L

luserXtrog

Even better:

#include <stdio.h>
#define from [(int []){
#define these }]
int main(void) {
    int A=0;
    printf("%d\n", A from 1,2,3,4,5 these );
return (int)0x401F00L;
}

Forgot the return!
 
S

Skybuck Flying

Haha good april's fool joke.

Though Bart is still gonna get banned since it wasn't april first yet ;)
unless he provides good example are admits he was mistaken ;)

Bye,
Skybuck.
 
S

Skybuck Flying

The intention ofcourse is to execute different code based on the value of A
like a case statement in pascal or a switch in C.

Examples:

A = 4;

A ? B = 1; C = 2; D = 3; E = 4; F = 5;

A ? Init(); Work(); Draw(); Next(); CleanUp();

Bye,
Skybuck.
 
L

luserXtrog

The intention ofcourse is to execute different code based on the value of A
like a case statement in pascal or a switch in C.

Both of those constructs already exist.
Examples:

A = 4;

A ? B = 1; C = 2; D = 3; E = 4; F = 5;

A ? Init(); Work(); Draw(); Next(); CleanUp();

This is psychotic.
What, in satan's glorious name, are you driving at?
Statements and Expressions are not the same.

No thanks for the fish, motherfucker?!
 
L

Lew Pitcher

The intention ofcourse is to execute different code based on the value of
A like a case statement in pascal or a switch in C.

Examples:

A = 4;

A ? B = 1; C = 2; D = 3; E = 4; F = 5;

A ? Init(); Work(); Draw(); Next(); CleanUp();

I suppose that you /could/ use the trinary operator in such a manner, but
that's like using a shoe to drive a nail: it is possible, but the tool
wasn't designed for that task, and there are tools that were specifically
designed for it.

If you are looking to "execute different code based on the value of A", a
switch() statement, or an if() statement, or even a convolution of a loop
and subscript would work much better, and (at least, for two of them)
are /specifically/ designed to do this work.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top