472,975 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 software developers and data experts.

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 1942
In article <7864a589-19a7-458d-99cd-
26**********@x41g2000hsb.googlegroups.com>, zh*********@gmail.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...@gmail.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...@x41g2000hsb.googlegroups.com>, zhang.xi...@gmail.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...@gmail.com>wrote:
On Mar 18, 10:57 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <7864a589-19a7-458d-99cd-
2611d772e...@x41g2000hsb.googlegroups.com>, zhang.xi...@gmail.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
In article <01db3ce8-2e5e-470d-a892-
0e**********@b1g2000hsg.googlegroups.com>, zh*********@gmail.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...@gmail.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.brettschnei...@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.brettschnei...@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
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
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...
36
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.