473,769 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

There is no "continue" for switch?

xz
What if I want the following:

vector<intv;

// v is loaded by push_back()

switch( v.size() ) {
case 2:
//do something

case 4:
//somehow delete two element
//and then do the same thing as in case 2

case 6:
//somehow delete two element
//and then do the same thing as in case 4

default:
//...
}
I thought using "continue" to implement this but got this:

continue statement not within a loop

Mar 18 '08
13 2011
In article <01db3ce8-2e5e-470d-a892-
0e**********@b1 g2000hsg.google groups.com>, zh*********@gma il.com says...

[ ... ]
well, I gave a case not so tough, what if a case a little tougher?

vector<intv;

// v is loaded by push_back()

switch( v.size() ) {
case 2:
//do something

case 4:
//somehow delete two element
//and then do the same thing as in case 2

case 6:
//somehow delete *four* element
//and then do the same thing as in case 2

default:
//...

}

i.e. I want to go to case 2 both after I treat the case 4 and case 6.
Truth to tell, I doubt I'd use switch at all for this. Rather, I'd do
something like this:

if (x == 2 || x == 4 || x == 6) {
v.erase(v.begin ()+2, v.begin()+x-2);
// do something
}
else
// whatever was in your default.

Of course, you'll probably need to make minor changes to account for the
position in the vector where you want to do the deletion.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 19 '08 #11
On Mar 19, 2:46 am, xz <zhang.xi...@gm ail.comwrote:
What if I want the following:

vector<intv;

// v is loaded by push_back()

switch( v.size() ) {
case 2:
//do something

case 4:
//somehow delete two element
//and then do the same thing as in case 2

case 6:
//somehow delete two element
//and then do the same thing as in case 4

default:
//...

}

I thought using "continue" to implement this but got this:

continue statement not within a loop
Hi Zhang,

It seems you wanna to something like spaghetti code can do. Are you
originally from Basic language? =) I think Junchen's approach is a
right way to do it.

Cheers,
Alex
Mar 19 '08 #12
On Mar 19, 9:32 am, Paul Brettschneider <paul.brettschn ei...@yahoo.fr>
wrote:
xz wrote:
What if I want the following:
vector<intv;
// v is loaded by push_back()
switch( v.size() ) {
case 2:
//do something
case 4:
//somehow delete two element
//and then do the same thing as in case 2
case 6:
//somehow delete two element
//and then do the same thing as in case 4
default:
//...
}
I thought using "continue" to implement this but got this:
continue statement not within a loop

Of course there is a continue for switch (for your example else-thread):

#include <iostream>

void f(uint8_t n)
{
std::cout << n << '\n';
switch(n) {
for(;;) {
case 2:
std::cout << "do_something\n ";
break;
case 4:
std::cout << "somehow delete 2 elements\n";
continue;
case 6:
std::cout << "somehow delete 4 elements\n";
continue;
default:
std::cout << "default\n" ;
break;
}
}

}

int main()
{
f(2);
f(4);
f(6);
f(1);

}

HTH,
Paul
I didn't know that ;)

Cheers,
Mar 19 '08 #13
On Mar 18, 6:42 pm, Paul Brettschneider <paul.brettschn ei...@yahoo.fr>
wrote:
Paul Brettschneider wrote:
Here is the correct solution (I think):
Yes; it works. Although I think using "goto" would have actually been
clearer here... at least with goto you know that it's going to jump
somewhere and you have to look for a label; whereas with your solution
I found that I first consulted the standard to verify that "continue"
was not for "switch" before looking very closely to see the "for (;;)"
hidden in there.
Jason

>
#include <iostream>

void f(int n)
{
std::cout << n << '\n';
for(;;) {
switch(n) {
case 2:
std::cout << "do_something\n ";
break;
case 4:
std::cout << "somehow delete 2 elements\n";
n -= 2;
continue;
case 6:
std::cout << "somehow delete 4 elements\n";
n -= 4;
continue;
default:
std::cout << "default\n" ;
break;
}
break;
}

}

int main()
{
f(2);
f(4);
f(6);
f(1);

}
Mar 19 '08 #14

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

Similar topics

14
3663
by: Daniel Bass | last post by:
is there an equivalent key word for C++'s "continue" in VB (.net) in this context? CString szLine; szLine = myReader.ReadLine(); while ( !szLine.IsEmpty() ) { if ( szLine(0) == '-' ) {
14
19418
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a strange workaround construct): -- do { // ...
36
2667
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 particularly useful. Thanks as usual.
0
9579
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7396
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.