continue with switch

V

v4vijayakumar

'continue' within switch actually associated with the outer 'while'
loop. Is this behavior protable?

int ch = '\n';
while (true) {
switch(ch) {
case '\n': cout << "test"; continue;
}
}

the above loop executed endlessly in "gcc version 2.96 20000731 (Red
Hat Linux 7.3 2.96-110)".
 
R

Richard Bos

v4vijayakumar said:
'continue' within switch actually associated with the outer 'while'
loop. Is this behavior protable?

int ch = '\n';
while (true) {
switch(ch) {
case '\n': cout << "test"; continue;
}
}

I have no idea if it's portable under C++, which is what your code
actually is. In this newsgroup we discuss ISO C, and yes, in that
language any continue statements do not apply to a switch statement, but
to the continue's closest surrounding loop statement, if there is any.

Richard
 
B

Bart Rider

v4vijayakumar said:
'continue' within switch actually associated with the outer 'while'
loop. Is this behavior protable?

int ch = '\n';
while (true) {
switch(ch) {
case '\n': cout << "test"; continue;
}
}

the above loop executed endlessly in "gcc version 2.96 20000731 (Red
Hat Linux 7.3 2.96-110)".

try this peace of code:
int main() {


int ch = 'a';
int i=0;
while (1) {
switch(ch) {
case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
case 'b': printf("i=%d, b\n",i); ch='c'; continue;
}
i++;

}
return 0;
}

you will see, that both printf-statements are executed. The continue
in switch is therefore to jump to the next case block, which is
executed *with* renewed comparison. The latter means, that if you
replace ch='b' by ch='d' or something, then only the first printf
is executed.
 
B

Bart Rider

Richard said:
I have no idea if it's portable under C++, which is what your code
actually is. In this newsgroup we discuss ISO C, and yes, in that
language any continue statements do not apply to a switch statement, but
to the continue's closest surrounding loop statement, if there is any.

Richard

really?
Any break inside a switch statement is actually assigned to the
switch statement (it leaves the choice statement switch and continues
with the first statement after switch) and not for any surrounding loop.
The same applies to continue IMHO.
 
R

Richard Heathfield

Bart Rider said:
Really.

Any break inside a switch statement is actually assigned to the
switch statement (it leaves the choice statement switch and continues
with the first statement after switch) and not for any surrounding loop.
The same applies to continue IMHO.

Really? Perhaps you'd better tell the ISO C Committee. I don't think they
took your opinion into account when signing off the C Standard. If you run,
you might still catch them.
 
P

pete

You've got a continue statement inside a while loop.
It's just as simple as that.
try this peace of code:
int main() {

int ch = 'a';
int i=0;
while (1) {
switch(ch) {
case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
case 'b': printf("i=%d, b\n",i); ch='c'; continue;
}
i++;

}
return 0;
}

you will see, that both printf-statements are executed. The continue
in switch is therefore to jump to the next case block, which is
executed *with* renewed comparison.

That's not what happens at all.
You put the ++i loop counter in the wrong place
and bypassed it with the continue statement.

i=1, a
i=2, b




/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int ch = 'a';
int i = 0;

while (1) {
++i;
switch(ch) {
case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
case 'b': printf("i=%d, b\n",i); ch='c'; continue;
default: return 0;
}
}
return 0;
}

/* END new.c */
 
F

Flash Gordon

Bart said:
try this peace of code:
int main() {

Better to be explicit about the lack of parameters:
int main(void)
int ch = 'a';
int i=0;
while (1) {
switch(ch) {
case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;

The break above is misleading and/or useless because it will never be
reached.
case 'b': printf("i=%d, b\n",i); ch='c'; continue;
}
i++;

}
return 0;
}

you will see, that both printf-statements are executed. The continue
in switch is therefore to jump to the next case block, which is
executed *with* renewed comparison.

This is misleading at best. The continue is a jump to the end of the
loop (not the beginning, important to not for do ... while loops).
Obviously it then goes back to the start of the loop in the above code
and executes the switch again. Where the continue goes to has absolutely
*nothing* to do with case blocks.
> The latter means, that if you
replace ch='b' by ch='d' or something, then only the first printf
is executed.

Your conclusion about your code is true. Add a printf before the switch
to see the rest is at best badly worded and in my opinion just plain wrong.

The description of continue in section 6.8.6.2 of the latest version of
the standard is very readable. You can find the latest draft at
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
 
V

v4vijayakumar

break/continue inside loop means
break-the-loop/continue-next-iteration, but inside switch (as it is not
associated with any looping) both could mean same thing, that is
execute-the-statement-next-to-switch.

am directly posting from google groups. can you please let me know,
what tool people generally use to read/post to groups. TIA.
 
P

pete

v4vijayakumar said:
break/continue inside loop means
break-the-loop/continue-next-iteration,
but inside switch (as it is not associated with any looping)
both could mean same thing, that is
execute-the-statement-next-to-switch.

No.

"continue" has no meaning related to "switch"
 
R

Richard Heathfield

v4vijayakumar said:
break/continue inside loop means
break-the-loop/continue-next-iteration, but inside switch (as it is not
associated with any looping) both could mean same thing, that is
execute-the-statement-next-to-switch.

No, that is incorrect. A break will break out of the immediately enclosing
switch or loop, it's true - but continue has nothing to do with switch. It
is purely a loop construct.
am directly posting from google groups.

Actually, you are /indirectly/ posting from Google Groups. What happens is
that you compose your message, and post it to a Google Web server using
HTTP on port 80. Google then automatically transmits that article to a news
server using NNTP on port 119. So it's actually slightly indirect.

can you please let me know,
what tool people generally use to read/post to groups. TIA.

I use KNode. Some people use tin, trn, Free Agent, Netscape Messenger - in
fact, any newsreader you like.
 
F

Flash Gordon

v4vijayakumar said:
break/continue inside loop means
break-the-loop/continue-next-iteration, but inside switch (as it is not
associated with any looping) both could mean same thing, that is
execute-the-statement-next-to-switch.

No, a continue has no meaning in a switch.
am directly posting from google groups. can you please let me know,
what tool people generally use to read/post to groups. TIA.

Almost and news client is better than Google, even Outlook Express,
although personally I use Thundirbird, I've also used Agent and
Sylpheed. Ask you ISP for details of your news server or search for a
free or pay for server.

However, when posting using Google is *is* possible to provide context,
so please do so. See the information about Google at and linked from
http://clc-wiki.net/wiki/Intro_to_clc
 
B

Bart Rider

pete said:
[...skip...]

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int ch = 'a';
int i = 0;

while (1) {
++i;
switch(ch) {
case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
case 'b': printf("i=%d, b\n",i); ch='c'; continue;
default: return 0;
}
}
return 0;
}

/* END new.c */

Was a little bit misleaded by my code.
No I see clear. Thanks for opening my eyes.

Bart
 
C

CBFalconer

v4vijayakumar said:
.... snip ...

am directly posting from google groups. can you please let me know,
what tool people generally use to read/post to groups. TIA.

Google groups is a very poor interface to Usenet. You can use the
methods described below in my sig. However you would be well
advised to install a real newsreader (such as Thunderbird from
mozilla.org) and access news directly through your ISP and a
newsserver. Most ISPs supply a newserver.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
V

void * clvrmnky()

Bart said:
really?
Any break inside a switch statement is actually assigned to the
switch statement (it leaves the choice statement switch and continues
with the first statement after switch) and not for any surrounding loop.
The same applies to continue IMHO.

Nope. continue only applies to loops.
 
A

Andrew Poelstra

Google groups is a very poor interface to Usenet. You can use the
methods described below in my sig. However you would be well
advised to install a real newsreader (such as Thunderbird from
mozilla.org) and access news directly through your ISP and a
newsserver. Most ISPs supply a newserver.

Just out of curiousity, have you ever had problems with Thunderbird
locking up? Whenever a large batch of messages came in, the
program would stop working permanently and require me to delete all
my mail files. I was using FC5.

Now I just use slrn, and I highly recommend it. The only issue I
have encountered is that I need to do my own line wrapping.
 
C

CBFalconer

Andrew said:
Just out of curiousity, have you ever had problems with Thunderbird
locking up? Whenever a large batch of messages came in, the
program would stop working permanently and require me to delete all
my mail files. I was using FC5.

Well, I actually use Netscape 4.75 for mail and news, because I
consider it better than T'bird for my purposes.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
D

Default User

CBFalconer said:
Google groups is a very poor interface to Usenet. You can use the
methods described below in my sig. However you would be well
advised to install a real newsreader (such as Thunderbird from
mozilla.org) and access news directly through your ISP and a
newsserver. Most ISPs supply a newserver.

Many ISPs these days have dropped usenet access, notably AOL. Mostly
dial-ups (in the US) have gotten rid of it, but some broadband
providers. If the OP is unable to get it from the ISP, then either a
free news service (I don't personally know of any) or a for-pay one
will be needed. I use news.individual.net, it costs 10 euro per year,
about $13 US.



Brian
 
A

Andrew Poelstra

Many ISPs these days have dropped usenet access, notably AOL. Mostly
dial-ups (in the US) have gotten rid of it, but some broadband
providers. If the OP is unable to get it from the ISP, then either a
free news service (I don't personally know of any) or a for-pay one
will be needed. I use news.individual.net, it costs 10 euro per year,
about $13 US.

I can't imagine anyone on c.l.c who uses AOL, but I've been
surprised before.
 
F

Flash Gordon

Default said:
Many ISPs these days have dropped usenet access, notably AOL. Mostly
dial-ups (in the US) have gotten rid of it, but some broadband
providers. If the OP is unable to get it from the ISP, then either a
free news service (I don't personally know of any) or a for-pay one
will be needed. I use news.individual.net, it costs 10 euro per year,
about $13 US.

There is a news group about free news servers...

Also I know of a couple of free servers.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
T

tedu

Bart said:
Any break inside a switch statement is actually assigned to the
switch statement (it leaves the choice statement switch and continues
with the first statement after switch) and not for any surrounding loop.
The same applies to continue IMHO.

did you try it?

void foo(int x) {
switch (x) {
case 0: continue;
case 1: break;
}
}

t.c: In function 'foo':
t.c:3: error: continue statement not within a loop
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top