if(question) || if question 
Author Message
 if(question) || if question

could any one ans this stupid question
why do we write
if (expression)
{
//some code

Quote:
}

and not
if expression
{
//some code
Quote:
}

I guess this should work (at least what my rudimentary knoledge
I see no benifit in (


Thu, 22 Jan 2004 06:28:45 GMT  
 if(question) || if question

Quote:

> could any one ans this stupid question
> why do we write
> if (expression)
> {
> //some code

> }
> and not
> if expression
> {
> //some code
> }

We write it that way because the syntax of an if statement in C is given
by

if_statement ::= if (expr) statement

Quote:
> I guess this should work

it doesn't

Joeri



Thu, 22 Jan 2004 06:45:16 GMT  
 if(question) || if question
Hi,


Quote:
> could any one ans this stupid question why do we write
> if (expression)
> { some code }
> and not
> if expression
> { some code }
> I guess this should work (at least what my rudimentary knoledge I see no
> benifit in (

So what does

    if  "Hello"  "there"  "!";

mean? On the other hand,

    if ("Hello") "there" "!";

is pretty clear.

Daniel

--
IMO, anyway.

clc FAQ:    http://www.eskimo.com/~scs/C-faq/top.html
08/05*  Bank Holiday in Scotland and Northern Ireland



Thu, 22 Jan 2004 06:44:48 GMT  
 if(question) || if question

Quote:
>could any one ans this stupid question
>why do we write
>if (expression)
>{
>//some code

>}
>and not
>if expression
>{
>//some code
>I guess this should work (at least what my rudimentary knoledge
>I see no benifit in (

FILE *f;
char buffer[1024];
char **current;
... give values to f and current here ...;

if fread(buffer, 1, sizeof buffer, f) == sizeof buffer*current = buffer;

is a little difficult to interpret without the parens.

                                        Gordon L. Burditt



Thu, 22 Jan 2004 06:57:10 GMT  
 if(question) || if question

Quote:
> I guess this should work (at least what my rudimentary knoledge
> I see no benifit in (

It's ambiguous.


Fri, 23 Jan 2004 01:45:17 GMT  
 if(question) || if question


Quote:
> could any one ans this stupid question
> why do we write
> if (expression)
> {
> //some code

> }
> and not
> if expression
> {
> //some code
> }
> I guess this should work (at least what my rudimentary knoledge
> I see no benifit in (

Consider the program fragment:

    if a == b * f(x);

Is this `if (a == b) then *f(x);` where the test is simple and the body
is non-trivial, or `if (a == (b * f(x)));` where the test is more complex
and the body is empty?

[Parsers usually don't do type-analysis, so it's cheating to analyse
bases on the return type of `f`; in any case this trick leads, IMAO,
to parsers even more fragile than the traditional must-have-typenames-NOW
C parser.]

[I tried to construct an example that was ambiguous even with full type
information, but evidently my brain hasn't rebooted since the weekend
yet.]

--
Chris "offline" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Fri, 23 Jan 2004 17:24:02 GMT  
 if(question) || if question

Quote:

> could any one ans this stupid question
> why do we write
> if (expression)
> {
> //some code

> }
> and not
> if expression
> {
> //some code
> }
> I guess this should work (at least what my rudimentary knoledge
> I see no benifit in (

As others have pointed out with specific examples, the condition
expression can be arbitrarily complex, and in some contexts it's
impossible to tell where the condition expression ends and the actual
statement body begins.  There needs to be *some* kind of delimiter to
separate the two.  If the grammar were rewritten so that an
if-statement body *must* be a compound statement (delimited by the '{'
and '}' characters), then the parens could be eliminated.


Fri, 23 Jan 2004 21:37:21 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. question question question

2. Not C program question, historical question

3. Question FAQ question

4. array question - beginner question

5. Solaris Unix Porting Question / Signals Question

6. A similar lint question (and questions about casts)

7. Newbie: CATALOG sample question (SIMPLE question)

8. Question: MDI question

9. This is a biztalk question but probably an easy question for you C# guru's

10. Non-Programming Question... experience question mostly

11. A question regarding FAQs question 12.2

12. Peer to peer board game - possible remoting question or design question

 

 
Powered by phpBB® Forum Software