Topic: Does "if ()" make any sense?


Author: David R Tribble <david.tribble@dallas.beasys.com>
Date: 1998/12/08
Raw View
References: <xjviufuwbj0.fsf@hvergelmir.ifi.uio.no>

Igor Rafienko wrote:
> If you have a piece of code under testing/non-portable code/etc.:
>     if (0) {
>       // do strange stuff
>     }
>
> Could be used as an alternative to
>     #if 0
>       // do strange stuff
>     #endif

Yes, except when the 'do strange stuff' contains references to
variables, functions, constants, or types that are not yet defined,
or that don't exist on the platform at hand.  There are many cases
where Igor's idea will work, but there are also many case where
simply replacing the preprocessor '#if' with an 'if' statement
won't work.

Some actual source code, for example:
    #if 0  // API func not designed yet!
        i = apiOpenThingy(name?, type?, ...);
        if (i == -1? or < 0?)
            ...error, clean up, return (-1);
    #else
        // Fake it for now
        i = 0;
    #endif

For situations like this, I usually use something that stands out
a little more:
    #if IS_INCOMPLETE___
        ...whatever...
    #endif

-- David R. Tribble, dtribble@technologist.com --



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1998/12/05
Raw View
Christopher Eltschka wrote in message
<3663B2BB.F2DA591C@physik.tu-muenchen.de>...
>Indeed, I don't understand why it is optional in for.
>It's far from obvious that for(;;) means "forever", not "never".
>Especially given that the default value of bool is false
>(or for C: the default value of int is 0).
>
>for(;bool();) would give a do-never "loop".


*** There are legitimate places when you want to loop forever. Why
would you have a concise way of expressing a loop that's never
entered?

Also don't forget it's around from the C days, so your argument is a
moot point.

>(or for C: the default value of int is 0).
What do you mean? Extern int? What's the link?

You don't have much of a case here. The fact that for (;;) enters an
infinite loop is obvious for anyone who's read an introductory book on
either C or C++.

Andrei




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: smeggie <smeglord@crackhore.com>
Date: 1998/12/02
Raw View
My question is:
there are many valid times to have a loop always run and run "infinately"
(assuming you break upon some situation)
while(1){
break;
}
but in the situation of a conditional why would you want it to always
execute?
doesn't that by definition void its conditional status?  I guess what I don't
get is why would it be relevant if "if( )" assumed true,false,error,crashed
or any other path?  if it was always true or false it wouldn't be conditional
and if it errored or crashed...
maybe someone can make sence of this for me.

thanx
Sam

Ron Natalie wrote:

> The issue is that
>         if() ...
>
> is a completely useless construct (regardless as to whether
> the expression is interpretted as true or false).
>
> The reason for takes optional arguments is that it is
> perfectly reasonable to write for loops with any of
> the three components (init, test, incr) omitted.  The
> fact that all three could be omitted is sort of an odd
> byproduct.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Igor Rafienko <igorr@ifi.uio.no>
Date: 1998/12/02
Raw View
smeggie <smeglord@crackhore.com> writes:

| My question is:
| there are many valid times to have a loop always run and run "infinately"
| (assuming you break upon some situation)
| while(1){
| break;
| }
| but in the situation of a conditional why would you want it to always
| execute?
| doesn't that by definition void its conditional status?  I guess what I don't
| get is why would it be relevant if "if( )" assumed true,false,error,crashed
| or any other path?  if it was always true or false it wouldn't be conditional
| and if it errored or crashed...
| maybe someone can make sence of this for me.

If you have a piece of code under testing/non-portable code/etc.:

if (0) {
  // do strange stuff
}


Could be used as an alternative to

#if 0

#endif


igorR
--
Vy vstaete s posteli, a lezhashaya ryadom devushka hvataet vas za ruku i
govorit:
- Save changes? Yes or No?


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/12/01
Raw View
The issue is that
 if() ...

is a completely useless construct (regardless as to whether
the expression is interpretted as true or false).

The reason for takes optional arguments is that it is
perfectly reasonable to write for loops with any of
the three components (init, test, incr) omitted.  The
fact that all three could be omitted is sort of an odd
byproduct.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/12/01
Raw View
AllanW@my-dejanews.com wrote:
>
> In article <73kupo$7sp$1@cscnews.csc.calpoly.edu>,
>   dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs) wrote:
> >
> > In article <365c2e46.0@10.1.1.65>,
> > Andrei Alexandrescu <alexandrescua@micromodeling.com> wrote:
> > >I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
> > >my surprise, no warnigs, no errors!
> > >
> > >void foo()
> > >{
> > >    if ()
> > >    return;
> > >}
>
> That's kind of humorous.
>
> However, we can surmise that it (incorrectly) treated the missing
> condition as "true", similar to a for statement without a condition.

It would be interesting to see if it allows the following constructs
as well:

bool b=; // missing bool expression assumed as true?
int i= ? 1 : 2; // gives 1?
bool x = && ; // two missing bool expressions; both true?

This would lead to very interesting code ;-)

[...]

> Then again, is there any reason why it shouldn't? What would you
> expect an if() statement to do -- act like if(true)? Act like
> if(false)? Either one of these can already be done trivially by
> typing out the expression. And anything else is so far from
> obvious that it would surprise the programmer.

Indeed, I don't understand why it is optional in for.
It's far from obvious that for(;;) means "forever", not "never".
Especially given that the default value of bool is false
(or for C: the default value of int is 0).

for(;bool();) would give a do-never "loop".


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/11/30
Raw View
In article <73kupo$7sp$1@cscnews.csc.calpoly.edu>,
  dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs) wrote:
>
> In article <365c2e46.0@10.1.1.65>,
> Andrei Alexandrescu <alexandrescua@micromodeling.com> wrote:
> >I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
> >my surprise, no warnigs, no errors!
> >
> >void foo()
> >{
> >    if ()
> >    return;
> >}

That's kind of humorous.

However, we can surmise that it (incorrectly) treated the missing
condition as "true", similar to a for statement without a condition.

> Here is what g++ had to say:
>
>    ANSI C++ forbids an empty condition for `if'

Yes, it certainly does.

Is there any good reason why it should? I would imagine that this
type of error is fairly rare (hopefully explaining why Microsoft
either shipped the compiler knowing about the error, or more likely
didn't even test for it in the first place).

Then again, is there any reason why it shouldn't? What would you
expect an if() statement to do -- act like if(true)? Act like
if(false)? Either one of these can already be done trivially by
typing out the expression. And anything else is so far from
obvious that it would surprise the programmer.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Pete Becker <petebecker@acm.org>
Date: 1998/11/26
Raw View
Igor Rafienko wrote:
>
> "Andrei Alexandrescu" <alexandrescua@micromodeling.com> writes:

> | I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
> | my surprise, no warnigs, no errors!
> |
> | void foo()
> | {
> |     if ()
> |     return;
> | }
> |
> | As I saw afterwards, the construction looks like if (true).
> |
> | Does this make any C++ sense?

> Remember the eternal loop?

> for( ; ; );

Interesting rationalization. But check the grammar for for: all three
parts are explicitly made optional. No such dispensation for if.


> It does - or at least according to B. Stroustrup's book.

> [snip]
> if ( expression ) statement
> ...

> The expression is evaluated and if it is nonzero, the first
> substatement if executed.
> [/snip]

However, this does not say that the expression is optional.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: nimel@my-dejanews.com
Date: 1998/11/26
Raw View
Andrei Alexandrescu wrote:
>
> I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
> my surprise, no warnigs, no errors!
>
> void foo()
> {
>     if ()
>     return;
> }

VC++ 6.0 does not accept the code above. Obviously a bug that has
been fixed. Next topic...

/Niklas Mellin

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs)
Date: 1998/11/28
Raw View
In article <365c2e46.0@10.1.1.65>,
Andrei Alexandrescu <alexandrescua@micromodeling.com> wrote:
>I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
>my surprise, no warnigs, no errors!
>
>void foo()
>{
>    if ()
>    return;
>}

Here is what g++ had to say:

   ANSI C++ forbids an empty condition for `if'


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/11/28
Raw View
Igor Rafienko wrote:
>
>
> What about for-loops as this one:
>
> for ( int i = 0; ; i++ );

Because the definition of for isn't:

 for(expr; expr; expr) statemen

its:
 for( statement optional-expr; optional-expr) statement

The expresssion is required for if.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1998/11/29
Raw View
Ron Natalie wrote in message <365EC9A4.CB8BDA6F@sensor.com>...
> for( statement optional-expr; optional-expr) statement
>
>The expresssion is required for if.


Actually it is:

for( simple-statement optional-expr; optional-expr) statement

(because you cannot have a compound statement in there)

Eh eh?

Thanks Ron.

Andrei



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Igor Rafienko <igorr@ifi.uio.no>
Date: 1998/11/25
Raw View
"Andrei Alexandrescu" <alexandrescua@micromodeling.com> writes:

| I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
| my surprise, no warnigs, no errors!
|
| void foo()
| {
|     if ()
|     return;
| }
|
| As I saw afterwards, the construction looks like if (true).
|
| Does this make any C++ sense?

Remember the eternal loop?

for( ; ; );


It does - or at least according to B. Stroustrup's book.

[snip]
if ( expression ) statement
...

The expression is evaluated and if it is nonzero, the first
substatement if executed.
[/snip]


() are not treated as unequal to zero. Or did I miss smth?

Anyhow, it's doubtfully a good idea to make it coding practice :)


igorR
--
Vy vstaete s posteli, a lezhashaya ryadom devushka hvataet vas za ruku i
govorit:
- Save changes? Yes or No?


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Igor Rafienko <igorr@ifi.uio.no>
Date: 1998/11/25
Raw View
Ron Natalie <ron@sensor.com> writes:

| Andrei Alexandrescu wrote:
| >
| > I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
| > my surprise, no warnigs, no errors!
| >
| > void foo()
| > {
| >     if ()
| >     return;
| > }
| >
| > As I saw afterwards, the construction looks like if (true).
|
| It should have given an error .  "if" requires an expression and
| while there are null statements, there isn't any such concept
| as a null expression.

What about for-loops as this one:

for ( int i = 0; ; i++ );


igorR
--
Vy vstaete s posteli, a lezhashaya ryadom devushka hvataet vas za ruku i
govorit:
- Save changes? Yes or No?



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/11/25
Raw View
Andrei Alexandrescu wrote:
>
> I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
> my surprise, no warnigs, no errors!
>
> void foo()
> {
>     if ()
>     return;
> }
>
> As I saw afterwards, the construction looks like if (true).

It should have given an error .  "if" requires an expression and
while there are null statements, there isn't any such concept
as a null expression.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/25
Raw View
Igor Rafienko wrote:

> Ron Natalie <ron@sensor.com> writes:

> | Andrei Alexandrescu wrote:
> | >
> | > I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
> | > my surprise, no warnigs, no errors!
> | >
> | > void foo()
> | > {
> | >     if ()
> | >     return;
> | > }
> | >
> | > As I saw afterwards, the construction looks like if (true).
> |
> | It should have given an error .  "if" requires an expression and
> | while there are null statements, there isn't any such concept
> | as a null expression.

> What about for-loops as this one:

> for ( int i = 0; ; i++ );

The grammar for a for statement is:

 for( for-init-statement conditionopt ; expressionopt) statement

Note that condition is explicitly optional; the same is not true for
if().


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/11/25
Raw View
Igor Rafienko <igorr@ifi.uio.no> writes:
>Ron Natalie <ron@sensor.com> writes:
>| Andrei Alexandrescu wrote:
>| >
>| > I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
>| > my surprise, no warnigs, no errors!
>| >
>| > void foo()
>| > {
>| >     if ()
>| >     return;
>| > }
>|
>| It should have given an error .  "if" requires an expression and
>| while there are null statements, there isn't any such concept
>| as a null expression.

>What about for-loops as this one:

>for ( int i = 0; ; i++ );

The for-statement and if-statement are different constructs,
and have different syntax rules.

For example:
        if ( int i = 0 ; i < 10 ; ++i ) ... // error
        for( int i = 0 ; i < 10 ; ++i ) ... // ok

An if-statement requires an expression in the parentheses. There
are no empty expressions.

In a for-statement header, all three parts are optional; only the
two semicolons are required.

        if (     ) ... // error
        for( ; ; ) ... // ok

---
Steve Clamage, stephen.clamage@sun.com

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1998/11/25
Raw View
I had an incomplete code snippet in VC++ 5.0 and compiled it by mistake. To
my surprise, no warnigs, no errors!

void foo()
{
    if ()
    return;
}

As I saw afterwards, the construction looks like if (true).

Does this make any C++ sense?

Andrei
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]