syntax ok?

M

m_schellens

emacs fails to hightlight the following line properly, when
I changed 'continue' to 'goto skip'.
So just to be sure there isn't any weired rule,
does the following do what I want?

if( yLog) if( y <= 0.0) goto skip; else y = log10( y);

Thanks,
marc
 
P

Phlip

m_schellens said:
emacs fails to hightlight the following line properly, when
I changed 'continue' to 'goto skip'.
So just to be sure there isn't any weired rule,
does the following do what I want?

if( yLog) if( y <= 0.0) goto skip; else y = log10( y);

It's well-formed. Please don't do it. Google for 'goto' to learn why. The
best way to prematurely exit a block is to return.

emacs obviously has a bug in its hiliter that bites code that most emacs
users would never write.
 
A

Andrew Koenig

It's well-formed. Please don't do it. Google for 'goto' to learn why. The
best way to prematurely exit a block is to return.
emacs obviously has a bug in its hiliter that bites code that most emacs
users would never write.

Moreover, whether it does what you want depends on what you want. Do you
want this?

if( yLog) { if( y <= 0.0) goto skip; else y = log10( y); }

or do you want this?

if( yLog) { if( y <= 0.0) goto skip; } else y = log10( y);

They're different. (Without the { }, the first one of these examples is
what you get)
 
U

ulrich

emacs fails to hightlight the following line properly, when
I changed 'continue' to 'goto skip'.
So just to be sure there isn't any weired rule,
does the following do what I want?

if( yLog) if( y <= 0.0) goto skip; else y = log10( y);

1. there is an incorrect semicolon after "skip", else cannot be a
standalone statement.

2. much clearer style would be:

if( yLog)
{
if( y <= 0.0)
{
goto skip;
}
else
{
y = log10( y);
}
}

this is what your code would do without the wrong semicolon.

3. re-think on how you could avoid the "goto"
 
C

Chris Theis

ulrich said:
1. there is an incorrect semicolon after "skip", else cannot be a
standalone statement.

You're right that "else" cannot be a standalone statement, but in this case
it isn't. The statement above could be rewritten as

if( yLog)
if( y <= 0.0)
goto skip;
else y = log10( y);
2. much clearer style would be:

if( yLog)
{
if( y <= 0.0)
{
goto skip;
}
else
{
y = log10( y);
}
}

this is what your code would do without the wrong semicolon.

The semicolon is all right. See Andrew's post regarding the effects of the
code.
3. re-think on how you could avoid the "goto"

I cannot agree more!!

Cheers
Chris
 
C

Clark S. Cox III

1. there is an incorrect semicolon after "skip", else cannot be a
standalone statement.

There is nothing wrong with that semicolon. The above line is equivalent to:

if( yLog)
if( y <= 0.0)
goto skip;
else
y = log10(y);

In fact, you have the exact same semicolon in your example...
 
U

ulrich

[...]
2. much clearer style would be:

if( yLog)
{
if( y <= 0.0)
{
goto skip;
}
else
{
y = log10( y);
}
}

this is what your code would do without the wrong semicolon.

The semicolon is all right. See Andrew's post regarding the effects of
the
code.

you're right. obviously i have typed my answer too quickly...

I cannot agree more!!

:)
 
M

mschellens

Thanks everybody.
As I said, just wanted to make sure because so far almost always emacs
C++ mode did something 'strange' there was a reason (ie. missing
semicolon etc).
I know about the use of goto, but in some few cases a goto can be
cleaner.
(for what it's worth: return was no option here since there are
statements following)
Thanks,
marc
 
L

Lionel B

Thanks everybody.
As I said, just wanted to make sure because so far almost always
emacs C++ mode did something 'strange' there was a reason (ie.
missing semicolon etc). I know about the use of goto, but in
some few cases a goto can be cleaner.

Cleaner? Gotos? wash your mouth out at once ;-)
(for what it's worth: return was no option here since there are
statements following)

FWIW, a return statement does not have to be the last statement in a
function (if that's what you were implying). You may also have as many
returns as you like.

int silly()
{
return 0;
std::cout << "Don't expect to see this...";
return 1;
std::cout << " ...or this";
return 2;
}

Anyway, I'm not sure that jumping out of functions at odd places via
returns is that much better an idea than gotos (needless to say I do it
myself all the time...).

Anyone remember "Structured Programming" (as taught, usually in Pascal
in the early 80's)?
 
M

m_schellens

.... the goto statement.
These statements have to be executed after the goto statement.
 

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,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top