Re: Encountering else without having executed an if, was: Re:
'if' statement to control switch case
On Apr 15, 5:51 pm, "James Kanze" <james.ka...@gmail.com> wrote:
If I write something like:
bool c = false ;
goto toto ;
if ( c ) {
toto:
stmt1 ;
} else {
stmt2 ;
}
what should happen? The condition is false; should the else
part be executed.
No. The program above would have to execute the if-statement with a
condition that yields false in order to transfer control to the
if-statement's associated "else" clause. But a goto statement in the abov=
e
program bypasses the if-statement - thereby preventing the if-statement f=
rom
executing at all. So the if-statement has no opportunity to evaluate its
condition - or to to transfer control to its associated else sub-statemen=
t -
simply by virtue of the fact that the if-statement itself is never execut=
ed.
Therefore the
only possible interpretation is that the condition result is effectiv=
ely
preserved.
How do you preserve it in the above. It hasn't ever been
evaluated.
Exactly. Since the if-statement is not executed, the if-statement selects
neither of its sub-statements for execution. In fact, it follows that the
only way for a C++ program to execute either sub-statment associate with =
an
unexecuted if-statement, is for the program to execute a goto statement t=
hat
transfers control into one of the sub-statement blocks directly.
The original issue was in code something like (from memory):
void
foo( bool b )
{
switch (1) {
if ( b ) {
case 1:
std::cout << "1 " ;
} else {
case 2:
std::cout << "2 " ;
}
}
}
int
main()
{
foo( false ) ;
foo( true ) ;
std::cout << '\n' ;
return 0 ;
}
Different compilers (and different versions of the same
compiler) were doing different things. One compiler, in
particular, output "1 2 1 2" (whereas most others output "1 1").
"1 1" is the expected output. It is not possible for the program to execu=
te
the else clause (that would output "2") because a) the case 2 branch of t=
he
switch statement is never equal to the value that the switch-statement's
condition yields, and b) no label is present within the else clause that
would permit a goto statement to transfer control into the else block
directly.
Given the language in the standard, I not sure I can say that
this compiler wrong.
I think that the current language in the Standard is enough to define the
correct behavior. And to avoid making a change to the (normative) text th=
at
may be interpreted as a change in the Standard that could affect the
behavior of existing C++ programs, I would recommend adding an example
instead. In particular, I would suggest an example that illustrates a got=
o
statement that transfers control into the first substatement of an
if-statement. The purpose of this example would be to show that the
else-clause is not executed under such circumstances:
[Example:
int x(1);
goto label1:
if (x == 0) {
label1:
x = 2;
}
else {
x = 3;
}
... // x has the value 2
--end example]
Greg
---
[ 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 ]