Topic: Encountering else without having executed an if


Author: Markus Moll <markus.moll@esat.kuleuven.be>
Date: Mon, 1 Oct 2007 12:57:48 CST
Raw View
Hi

James Kanze wrote:
> On Apr 12, 5:11 pm, com...@panix.com (Greg Comeau) wrote:
>> In article <1176380229.125484.70...@y5g2000hsa.googlegroups.com>,
>
>> James Kanze <james.ka...@gmail.com> wrote:
>>> ... the actual wording in
>>> C++ standard seems to require the test a second time: it says
>>> quite clearly that "If the else part of the selection statement
>>> is present and the condition yields false, the second
>>> substatement is executed."

I disagree with your interpretation. If the else part was mandatory, it
would probably read:
"If the condition (6.4) yields true the first substatement is executed.
  If the condition yields false, the second substatement is executed."

The addition "If the else part of the selection statement is present and
[...]" is meant to describe what happens if there is no else part (=>
nothing).

But with the modified version above you would probably not draw the same
conclusions.

More importantly, the whole "if (condition) statement else statement" is
a single statement. Therefore, you cannot first execute the
if-"statement", then the else-"statement". There is only one statement,
although it exists in different forms:
(6.4)
| selection-statement:
|            if ( condition ) statement
|            if ( condition ) statement else statement
|            switch ( condition ) statement

> To tell the truth, I'm not too sure myself.  The problem is
> trying to figure out what should happen, according to the
> standard, if you enter the if statement by jumping over the
> evaluation of the condition.

This is a different problem, indeed. One that is already solved in C:
"In the else form, the second substatement is executed if the expression
compares equal to 0. If the    rst substatement is reached via a label,
the second substatement is not executed." (C99 draft (N843), 6.8.4.1 (2))

The interesting question is whether this is a rather recent addition or
if it was simply forgotten in C++. IMO this one sentence should be added
to C++0x.

> The words I quoted could be taken
> to suggest that on encountering the else, the condition must be
> evaluated in order to determine whether the else part is to be
> executed or not.

As I said, IMO there is no "encountering the else".

Markus

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]