(boolean_expression) ? statement;

V

v4vijayakumar

why this isn't valid c?

(boolean_expression) ? statement;

whereas the following code is valid.

if(boolean_expression) statement;
 
M

Marc Boyer

Le 07-06-2006 said:
why this isn't valid c?

(boolean_expression) ? statement;

whereas the following code is valid.

if(boolean_expression) statement;


Because 'nop' is a valid instruction but 'nothing' is not
a valid value ?

Marc Boyer
 
V

Vladimir Oka

v4vijayakumar said:
why this isn't valid c?

(boolean_expression) ? statement;

whereas the following code is valid.

if(boolean_expression) statement;

Are you serious asking this?

The syntax for ?: operator is (to put it simply):

expression ? statement1 : statement2;

and not:

expression ? statement;

And the reason why is because C language defines it that way.

If you yearn for it, I guess you can always leave `statement2` empty,
but the `:` is mandatory.
 
F

Flash Gordon

Vladimir said:
Are you serious asking this?

The syntax for ?: operator is (to put it simply):

expression ? statement1 : statement2;

Wrong. It is expressions not statements.
and not:

expression ? statement;

And the reason why is because C language defines it that way.

If you yearn for it, I guess you can always leave `statement2` empty,
but the `:` is mandatory.

No, that would not work, because there is no such thing as an empty
expression.

You can't do it for the same reason you can't do
3 +;
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Vladimir said:
Are you serious asking this?

The syntax for ?: operator is (to put it simply):

expression ? statement1 : statement2;
Actually it is
logical-OR-expression ? expression : conditional-expression
If you yearn for it, I guess you can always leave `statement2` empty,
but the `:` is mandatory.

Gvien the above, you cannot leave the second expression "empty".
 
V

Vladimir Oka

Flash said:
Wrong. It is expressions not statements.


No, that would not work, because there is no such thing as an empty
expression.

You can't do it for the same reason you can't do
3 +;

D'oh! I should have left my stupid hat at home today. I'll disappear
now for a while...
 
V

Vladimir Oka

Vladimir said:
D'oh! I should have left my stupid hat at home today. I'll disappear
now for a while...

On a "poor attempt at redemption" note, `?:` can be used as
`if-then-else` through C's feature that expressions are statements (and
you don't need to collect their results), and achieve that through side
effects:

/* simulates `if-then-else` */
(x == 5) ? printf("x is 5\n") : printf("x is not 5\n");

In which case, either of the "then" or "else" parts can be "empty" as
in "lacking side effects:

/* simulates `if-then` */
(x == 5) ? y = x, printf("x is 5\n") : 42;

As OP tried to equate `?:` to `if-then-else` I assumed he was after
something of the above sort. That's possibly why I messed my first
reply up so horribly.
 
C

Chris Dollin

v4vijayakumar said:
why this isn't valid c?

(boolean_expression) ? statement;

Because the conditional expression (a) requires three operands,
not two, and (b) all the operands are statements, not expressions.

C's like that because that's how it was designed. Other languages make
other design choices. For example, in Pop11 there's no syntactic
distinction between an expression and a statement [1], the if-construct
works perfectly happily as a conditional expression, and

if booleanExpression then someStatement endif

is a perfectly good expression (for values of "perfectly" that aren't
quite as perfect as one would like, but which we tried to address in
OpenSpice).

[1] I simplify, but only slightly.
 
G

Graham J Lee

On a "poor attempt at redemption" note, `?:` can be used as
`if-then-else` through C's feature that expressions are statements (and
you don't need to collect their results), and achieve that through side
effects:

/* simulates `if-then-else` */
(x == 5) ? printf("x is 5\n") : printf("x is not 5\n");

True, but I'd expect (just stylistically, obviously YMMV) to see
something more like:

printf("x is %s5\n",(x==5)?"":"not ");

to make use of the fact that ?: is controlling one expression, not the
whole statement.
 
V

Vladimir Oka

Graham said:
True, but I'd expect (just stylistically, obviously YMMV) to see
something more like:

printf("x is %s5\n",(x==5)?"":"not ");

to make use of the fact that ?: is controlling one expression, not the
whole statement.

I was just trying to force an example. A possibly "better" one may be:

(x == 42) ? you_found_it() : keep_looking();

Personally, I doubt I'd ever use any of those.

[As an aside: an early version of our coding standard forbade `?:`
outright. No one was ever able to explain to me exactly why would it
*always* be the wrong thing to use.]
 
M

mdler

Hello

a good example for this is inside a statement.
There you can not use an if construction.
ladimir Oka schreef:
{
int i;
for (i=0; i < 10; i++)
{
printf("I have %d %s and %d %s\n",i,i==1?"apple":"apples", i+1,
(i+1)==1?"melon":"melons");
}
}

A other thing is for NULL pointers during output
like printf("%s",pP?pP->name:"null pointer");

So this operation can be used as an if .. else
but should be used as an 'inline if'

Greetings
 
C

Chris Dollin

Vladimir said:
I was just trying to force an example. A possibly "better" one may be:

(x == 42) ? you_found_it() : keep_looking();

(x == 42 ? you_found_it : keep_looking)()
Personally, I doubt I'd ever use any of those.

I would. I likely have.
 
K

Keith Thompson

Chris Dollin said:
Because the conditional expression (a) requires three operands,
not two, and (b) all the operands are statements, not expressions.

Correction: the operands are expressions, not statements.
 
K

Keith Thompson

v4vijayakumar said:
why this isn't valid c?

(boolean_expression) ? statement;

whereas the following code is valid.

if(boolean_expression) statement;

The simplest answer, which several people have given, is that it's not
valid because the standard doesn't say it's valid. You might as well
ask why

(boolean_expression) \ $$# @ 42.Z <>

isn't valid.

But I suspect the basis for your question is the fact that

if (condition) foo; else bar;

can sometimes be written as

condition ? foo : bar

There is no such equivalent for an if statement with no else, because
there's simply no reason to have one. The purpose of the ?: operator
is to embed an if-then-else in an expression. Embedding an if-then
with no else in an expression doesn't make any sense. A statement can
either do something or do nothing; an expression must *always* yield a
value.

In either case, if you're not embedding it in an expression using an
"if" statement is much more straightforward than using the ?:
operator. If you have a choice between an "if" statement and a ?:
operator, use the if statement; use ?: only when it's actually
necessary.
 
B

Ben Pfaff

mdler said:
printf("I have %d %s and %d %s\n",i,i==1?"apple":"apples", i+1,
(i+1)==1?"melon":"melons");

This is OK for a one-off, but it breaks internationalization. As
the GNU gettext manual says:

Looking through Unix source code before the time anybody thought
about internationalization (and, sadly, even afterwards) one can often
find code similar to the following:

printf ("%d file%s deleted", n, n == 1 ? "" : "s");

After the first complaints from people internationalizing the code
people either completely avoided formulations like this or used strings
like `"file(s)"'. Both look unnatural and should be avoided. First
tries to solve the problem correctly looked like this:

if (n == 1)
printf ("%d file deleted", n);
else
printf ("%d files deleted", n);

But this does not solve the problem. It helps languages where the
plural form of a noun is not simply constructed by adding an `s' but
that is all. Once again people fell into the trap of believing the
rules their language is using are universal. But the handling of plural
forms differs widely between the language families. For example, Rafal
Maszkowski `<[email protected]>' reports:

In Polish we use e.g. plik (file) this way:
1 plik
2,3,4 pliki
5-21 pliko'w
22-24 pliki
25-31 pliko'w
and so on (o' means 8859-2 oacute which should be rather okreska,
similar to aogonek).
 
N

Neroku

Flash Gordon ha escrito:
Wrong. It is expressions not statements.

Think of expressions as a operands and operators (if any) that are
evaluated to a value, and think of statements as instructions that are
executed.

And now:

expression1 ? expression2 : expression3

You will understand why
return a > b ? 1 : 0;
works, but:
a > b ? return 1 : return 0;
doesn't.
 
C

Chris Torek

(This is drifting off topic but what the heck)

This is OK for a one-off, but it breaks internationalization. As
the GNU gettext manual says: [much snippage]
... For example, Rafal Maszkowski `<[email protected]>' reports:

In Polish we use e.g. plik (file) this way:
1 plik
2,3,4 pliki
5-21 pliko'w
22-24 pliki
25-31 pliko'w
and so on (o' means 8859-2 oacute which should be rather okreska,
similar to aogonek).

Clearly what is needed is something akin to terminfo, in which the
text translations have access to the numeric parameter(s) and can
perform arithmetic. For instance, something like this might work:

found %p1%d %{p1 1 = if text "plik" else {
p1 10 % 4 < if text "pliki" else text "pliko'w" } }

Here "%" introduces some kind of access or conversion, and %{ ... }
encloses "long form" or "readable" versions that require using the
"text" keyword to emit text. This might be equivalent to the
"short form" version:

found %p1%d %p1%1%=%?plik%:%p1%10%m%4%<%?pliki%:pliko'w

In either case, the C code call would be something like:

emit("found %d files", nfiles);

where the string literal ("found %d files") is also the key into
the database, and the number of parameters is probably limited to
some smallish constant (no more than 50, say). The string literal
can only contain "simple" conversion specifiers -- which need not
match one-for-one with print conversions -- that serve to identify
the variadic arguments for storage, for later access via the "pN"
parameter access strings. (The string literal could also serve
as a backup "emergency" string in case the translation database
is not available.)

(Not everyone likes RPN-like stack-y languages, of course, and the
use of %m for mod above is just to allow %% to represent a literal
percent sign. This is not intended to be a fully-thought-out proposal.)

(I suspect some will object to "inefficiency" in having to turn the
literal into a database search key. There is also some possibility
that multiple different replacement strings might map to one single
source string in English.)
 
B

Ben Pfaff

Chris Torek said:
Clearly what is needed is something akin to terminfo, in which the
text translations have access to the numeric parameter(s) and can
perform arithmetic. For instance, something like this might work:

I just want to add one thing (and then I'll let it drop), which
is that the same page in the gettext manual also describes a
solution to the problem.
 
V

Vladimir Oka

Chris said:
(x == 42 ? you_found_it : keep_looking)()


I would. I likely have.

Tastes differ. I find the above quite unreadable (both variants).
I'm not saying `?:` has no uses whatsoever, though.
 

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
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top