473,753 Members | 6,232 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 #1
13 2009
In article <7864a589-19a7-458d-99cd-
26**********@x4 1g2000hsb.googl egroups.com>, zh*********@gma il.com
says...
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:
//...
}
Correct -- continue works for loops by not switches. Fortunately you
don't need anything quite that difficult:

switch (v.size()) {
case 6:
// somehow delete two element (sic)
case 4:
// somehow delete two element
case 2:
// do something
}

If there isn't a break at the end of a block, execution will flow
through to the next block...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 18 '08 #2
On 3ÔÂ18ÈÕ, ÏÂÎç11ʱ46·Ö, xz <zhang.xi...@gm ail.com>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
please detail your intent.
Mar 18 '08 #3
xz
On Mar 18, 10:57 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <7864a589-19a7-458d-99cd-
2611d772e...@x4 1g2000hsb.googl egroups.com>, zhang.xi...@gma il.com
says...
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:
//...
}

Correct -- continue works for loops by not switches. Fortunately you
don't need anything quite that difficult:

switch (v.size()) {
case 6:
// somehow delete two element (sic)
case 4:
// somehow delete two element
case 2:
// do something

}

If there isn't a break at the end of a block, execution will flow
through to the next block...
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.

>
--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 18 '08 #4
On 3ÔÂ19ÈÕ, ÉÏÎç12ʱ21·Ö, xz <zhang.xi...@gm ail.com>wrote:
On Mar 18, 10:57 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <7864a589-19a7-458d-99cd-
2611d772e...@x4 1g2000hsb.googl egroups.com>, zhang.xi...@gma il.com
says...
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:
//...
}
Correct -- continue works for loops by not switches. Fortunately you
don't need anything quite that difficult:
switch (v.size()) {
case 6:
// somehow delete two element (sic)
case 4:
// somehow delete two element
case 2:
// do something
}
If there isn't a break at the end of a block, execution will flow
through to the next block...

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.


--
Later,
Jerry.
The universe is a figment of its own imagination.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
void f()
{
// do something
}

....
vector<intv;
// v is loaded by push_back()
switch( v.size() ) {
case 2:
f();//do something
break;
case 4:
//somehow delete two element
f();//and then do the same thing as in case 2
break;
case 6:
//somehow delete *four* element
f();//and then do the same thing as in case 2
break;

default:
//...
}

Mar 18 '08 #5
well, I gave a case not so tough, what if a case a little tougher?

vector<intv;

// v is loaded by push_back()
while(v.size() != 2)
{
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.

Or if you _always_ want to delete elements until the size is 2, I
wouldn't use a switch at all.

while(v.size() != 2)
{
// Determine which element to delete
// delete it
}

Mar 18 '08 #6
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
I believe you should rearrange your clauses and make '6' and
'4' fall through to the '2':

switch (v.size()) {
case 6:
// somehow delete 2 elements
// and FALL THROUGH
case 4:
// somehow delete 2 elements
// and FALL THROUGH
case 2:
// do something
break; // Yes, only here

default:
//...
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 18 '08 #7
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
Mar 18 '08 #8
Paul Brettschneider 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);
}
Of course this doesn't do what you what it to do - it doesn't recompute the
label to jump to. Sorry, I'm tired.
Mar 18 '08 #9
Paul Brettschneider wrote:
Paul Brettschneider 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);
}

Of course this doesn't do what you what it to do - it doesn't recompute
the label to jump to. Sorry, I'm tired.
Here is the correct solution (I think):

#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 18 '08 #10

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
19414
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
2662
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
9072
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
8896
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
9653
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
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...
1
9421
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9333
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
6869
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
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2284
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.