468,792 Members | 1,724 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,792 developers. It's quick & easy.

continue with switch

'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)".

May 12 '06 #1
25 17477
"v4vijayakumar" <v4***********@yahoo.com> wrote:
'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
May 12 '06 #2
v4vijayakumar wrote:
'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.
May 12 '06 #3
Richard Bos wrote:
"v4vijayakumar" <v4***********@yahoo.com> wrote:

'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


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.
May 12 '06 #4
Bart Rider 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?


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 12 '06 #5
Bart Rider wrote:

v4vijayakumar wrote:
'continue' within switch actually associated with the outer 'while'
loop. Is this behavior protable?
while (true) {
switch(ch) { continue; }
}

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 */
--
pete
May 12 '06 #6
Bart Rider wrote:
v4vijayakumar wrote:
'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() {


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/wg...docs/n1124.pdf
--
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
May 12 '06 #7
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.

May 12 '06 #8
v4vijayakumar wrote:

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"

--
pete
May 12 '06 #9
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 12 '06 #10
v4vijayakumar wrote:
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
--
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
May 12 '06 #11
pete wrote:
Bart Rider wrote:
v4vijayakumar wrote:
'continue' within switch actually associated with the outer 'while'
loop. Is this behavior protable?

[...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
May 12 '06 #12
Bart Rider wrote:
Richard Bos wrote:
"v4vijayakumar" <v4***********@yahoo.com> wrote:

'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


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.
May 12 '06 #13
v4vijayakumar wrote:
.... 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/>

May 12 '06 #14
On 2006-05-12, CBFalconer <cb********@yahoo.com> wrote:
v4vijayakumar wrote:

... 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.


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.
May 12 '06 #15
CBFalconer wrote:
v4vijayakumar wrote:

... 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.


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
May 12 '06 #16
On 2006-05-12, Default User <de***********@yahoo.com> wrote:
CBFalconer wrote:
v4vijayakumar wrote:
>

... 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.


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.
May 12 '06 #17
Default User wrote:
CBFalconer wrote:
v4vijayakumar wrote:
... 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.


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
May 12 '06 #18
Bart Rider wrote:
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

May 12 '06 #19
Flash Gordon <sp**@flash-gordon.me.uk> writes:
[...]
There is a news group about free news servers...


Which one is that?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 12 '06 #20
Bart Rider <ca*******@killamail.com> writes:
[...]
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.


I'm afraid that your opinion, humble or otherwise, has no bearing on
the question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 12 '06 #21
Andrew Poelstra wrote:
On 2006-05-12, CBFalconer <cb********@yahoo.com> wrote:
v4vijayakumar wrote:

... 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.


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/>

May 12 '06 #22

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Flash Gordon <sp**@flash-gordon.me.uk> writes:
[...]
There is a news group about free news servers...


Which one is that?


It's useless but called: alt.free.newservers. Your better off with free
NNTP listings on various websites. GIYF.
Rod Pemberton
May 12 '06 #23
Keith Thompson wrote:
Flash Gordon <sp**@flash-gordon.me.uk> writes:
[...]
There is a news group about free news servers...


Which one is that?


JGFI ;-)

It's alt.free.newsservers and Google carries it.

I think the usefulness of the group varies, but there are still people
there who will help anyone that wants a legally free news account.
--
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
May 12 '06 #24
Default User wrote:
CBFalconer wrote:
v4vijayakumar wrote:

... 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.


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 don't think many really consider AOL a viable ISP. As far as I
am concerned without news service a <whatever> service provider is
not providing Internet service. Many ISPs farm their news server
portion out to giganews, and I believe individuals can also get
such service at a fair price if they are really stuck with a non-I
SP.

Ask before signing up to any ISP.

--
"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/>
May 13 '06 #25
On Fri, 12 May 2006 09:21:47 UTC, "v4vijayakumar"
<v4***********@yahoo.com> wrote:
'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


That is what you asks for.

You should change while (true) to something that you can change when
you have to leave the while loop.

In switch 'continue' means simply leave the current work on the switch
and iterate to the next while. As the while says to run endless so the
program continues again with the switch.

In switch 'break' says leave off the work inside switch and continue
with the next statement immediately after the switch block it would
end as above because the next statement says that while() ends.
Iterating the while loop agains means execute the switch because true
will never been false, so while runs for ever.

So your program does exactly what you says it should do: run for ever.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
May 13 '06 #26

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael Satterwhite | last post: by
5 posts views Thread by viza | last post: by
4 posts views Thread by Christopher Benson-Manica | last post: by
1 post views Thread by jerico | last post: by
13 posts views Thread by xz | last post: by
36 posts views Thread by mdh | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.