473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18354
"v4vijayaku mar" <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:
"v4vijayaku mar" <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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2681
by: Michael Satterwhite | last post by:
I *MUST* be overlooking something obvious. Consider the following code: foreach($_POST as $key=>$value) { print "$key=>$value<br />"; if(! empty($value)) { switch($key) { case "Submit": case "keyList": case "curKey": print "Matched exception<br />";
5
33691
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
4
4657
by: Christopher Benson-Manica | last post by:
Given the following snippet, int i; for( i=0 ; i < 10 ; i++ ) { switch( i ) { case 0: case 1: continue; /* NB */ default: /* do something */
1
7831
by: jerico | last post by:
Hi.Could any one explain me the following program: int main() { char input="SWILTECH1\1\1"; int i,c; for(i=0;(c=input)!='\0';i++) { switch(c) {
13
1985
by: xz | last post by:
What if I want the following: vector<intv; // v is loaded by push_back() switch( v.size() ) { case 2: //do something
36
2618
by: mdh | last post by:
May I ask the group this somewhat non-focused question....having now seen "continue" used in some of the solutions I have worked on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind) Is there a good principle/s for the good usage of "continue"...or...do any of the regular/irregular contributors have a "favorite" way in which it is...
0
7473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7661
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7433
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5340
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1891
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
712
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.