There is no "continue" for switch?

X

xz

What if I want the following:

vector<int> v;

// 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
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
says...
What if I want the following:

vector<int> v;

// 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...
 
J

Junchen WANG

What if I want the following:

vector<int> v;

// 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.
 
X

xz

(e-mail address removed)>, (e-mail address removed)
says...










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<int> v;

// 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.
 
J

Junchen WANG

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

vector<int> v;

// 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.






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

- ÏÔʾÒýÓõÄÎÄ×Ö -

void f()
{
// do something
}

....
vector<int> v;


// 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:
//...
}
 
C

Christopher

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

vector<int> v;

// 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
}
 
V

Victor Bazarov

xz said:
What if I want the following:

vector<int> v;

// 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
 
P

Paul Brettschneider

xz said:
What if I want the following:

vector<int> v;

// 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
 
P

Paul Brettschneider

Paul said:
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.
 
P

Paul Brettschneider

Paul said:
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);
}
 
J

Jerry Coffin

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

vector<int> v;

// 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.
 
A

Alexander Dong Back Kim

What if I want the following:

vector<int> v;

// 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
 
A

Alexander Dong Back Kim

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,
 
J

jason.cipriani

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top