Connecting Tech Pros Worldwide Help | Site Map

There is no "continue" for switch?

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 18th, 2008, 03:55 PM
xz
Guest
 
Posts: n/a
Default There is no "continue" for switch?

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


  #2  
Old March 18th, 2008, 04:05 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

In article <7864a589-19a7-458d-99cd-
2611d772e4bd@x41g2000hsb.googlegroups.com>, zhang.xi.cn@gmail.com
says...
Quote:
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.
  #3  
Old March 18th, 2008, 04:05 PM
Junchen WANG
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

On 3月18日, 下午11时46分, xz <zhang.xi...@gmail.com>wrote:
Quote:
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.
  #4  
Old March 18th, 2008, 04:25 PM
xz
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

On Mar 18, 10:57 am, Jerry Coffin <jcof...@taeus.comwrote:
Quote:
In article <7864a589-19a7-458d-99cd-
2611d772e...@x41g2000hsb.googlegroups.com>, zhang.xi...@gmail.com
says...
>
>
>
Quote:
What if I want the following:
>
Quote:
vector<intv;
>
Quote:
// v is loaded by push_back()
>
Quote:
switch( v.size() ) {
case 2:
//do something
>
Quote:
case 4:
//somehow delete two element
//and then do the same thing as in case 2
>
Quote:
case 6:
//somehow delete two element
//and then do the same thing as in case 4
>
Quote:
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.

Quote:
>
--
Later,
Jerry.
>
The universe is a figment of its own imagination.
  #5  
Old March 18th, 2008, 04:35 PM
Junchen WANG
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

On 3月19日, 上午12时21分, xz <zhang.xi...@gmail.com>wrote:
Quote:
On Mar 18, 10:57 am, Jerry Coffin <jcof...@taeus.comwrote:
>
>
>
Quote:
In article <7864a589-19a7-458d-99cd-
2611d772e...@x41g2000hsb.googlegroups.com>, zhang.xi...@gmail.com
says...
>
Quote:
Quote:
What if I want the following:
>
Quote:
Quote:
vector<intv;
>
Quote:
Quote:
// v is loaded by push_back()
>
Quote:
Quote:
switch( v.size() ) {
case 2:
//do something
>
Quote:
Quote:
case 4:
//somehow delete two element
//and then do the same thing as in case 2
>
Quote:
Quote:
case 6:
//somehow delete two element
//and then do the same thing as in case 4
>
Quote:
Quote:
default:
//...
}
>
Quote:
Correct -- continue works for loops by not switches. Fortunately you
don't need anything quite that difficult:
>
Quote:
switch (v.size()) {
case 6:
// somehow delete two element (sic)
case 4:
// somehow delete two element
case 2:
// do something
>
Quote:
}
>
Quote:
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.
>
>
>
>
>
Quote:
--
Later,
Jerry.
>
Quote:
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:
//...
}

  #6  
Old March 18th, 2008, 05:26 PM
Christopher
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

Quote:
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)
{
Quote:
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:
//...
>
}
}
Quote:
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
}

  #7  
Old March 18th, 2008, 09:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

xz wrote:
Quote:
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


  #8  
Old March 18th, 2008, 10:35 PM
Paul Brettschneider
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

xz wrote:
Quote:
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
  #9  
Old March 18th, 2008, 10:45 PM
Paul Brettschneider
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

Paul Brettschneider wrote:
Quote:
xz wrote:
>
Quote:
>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.
  #10  
Old March 18th, 2008, 10:45 PM
Paul Brettschneider
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

Paul Brettschneider wrote:
Quote:
Paul Brettschneider wrote:
>
Quote:
>xz wrote:
>>
Quote:
>>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);
}

  #11  
Old March 19th, 2008, 02:15 AM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

In article <01db3ce8-2e5e-470d-a892-
0e0771bc98ff@b1g2000hsg.googlegroups.com>, zhang.xi.cn@gmail.com says...

[ ... ]
Quote:
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.
  #12  
Old March 19th, 2008, 04:45 AM
Alexander Dong Back Kim
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

On Mar 19, 2:46 am, xz <zhang.xi...@gmail.comwrote:
Quote:
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
  #13  
Old March 19th, 2008, 04:45 AM
Alexander Dong Back Kim
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

On Mar 19, 9:32 am, Paul Brettschneider <paul.brettschnei...@yahoo.fr>
wrote:
Quote:
xz wrote:
Quote:
What if I want the following:
>
Quote:
vector<intv;
>
Quote:
// v is loaded by push_back()
>
Quote:
switch( v.size() ) {
case 2:
//do something
>
Quote:
case 4:
//somehow delete two element
//and then do the same thing as in case 2
>
Quote:
case 6:
//somehow delete two element
//and then do the same thing as in case 4
>
Quote:
default:
//...
}
>
Quote:
I thought using "continue" to implement this but got this:
>
Quote:
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,
  #14  
Old March 19th, 2008, 05:25 AM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: There is no "continue" for switch?

On Mar 18, 6:42 pm, Paul Brettschneider <paul.brettschnei...@yahoo.fr>
wrote:
Quote:
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

Quote:
>
#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);
>
}
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.