Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > continue with switch

Reply
Thread Tools

continue with switch

 
 
v4vijayakumar
Guest
Posts: n/a
 
      05-12-2006
'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)".

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-12-2006
"v4vijayakumar" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Bart Rider
Guest
Posts: n/a
 
      05-12-2006
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.
 
Reply With Quote
 
Bart Rider
Guest
Posts: n/a
 
      05-12-2006
Richard Bos wrote:
> "v4vijayakumar" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-12-2006
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)
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-12-2006
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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-12-2006
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
 
Reply With Quote
 
v4vijayakumar
Guest
Posts: n/a
 
      05-12-2006
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.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-12-2006
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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-12-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
do{switch(0)default:{/*break or continue*/;}/*cleanup*/}while(0); Francois Grieu C Programming 55 07-30-2011 11:30 AM
There is no "continue" for switch? xz C++ 13 03-19-2008 05:21 AM
switch/continue Christopher Benson-Manica C Programming 4 06-08-2004 04:03 PM
bridge / layer 2 switch / layer 3 switch Joel M. Baldwin Cisco 2 11-06-2003 11:19 PM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM



Advertisments