473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

can't understand this weird case

Hi,guys!
I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I
meet a weird case:
bool Postorder::next() {
switch (pc)
case START:
while (true)
if (!child()) {
pc = LEAF;
return true;
case LEAF:
while (true)
if (sibling())
break;
else if (parent()) {
pc = INNER;
return true;
case INNER: ;
} else {
pc = DONE;
case DONE: return false;
}
}
}
I can't understand how the case goes, those cases are not even in the
same level, the author does not explain how the cases execute, could you
please help me with this? Any help is appreciated, thanks!
Jul 10 '06 #1
5 2389
"Frederick Dean" <di*******@tom.comwrote in message
news:e8**********@news.cn99.com...
: Hi,guys!
: I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I
: meet a weird case:
[...]
: I can't understand how the case goes, those cases are not even in the
: same level, the author does not explain how the cases execute, could you
: please help me with this? Any help is appreciated, thanks!

The loss of indentation in your post makes it even more difficult
to read this code.
I would guess that this example intends to illustrate that
the "case" labels of a switch statement are pretty much like
the labels of a goto: they can be used to jump anywhere within
other bracketed statements
[provided that no variable initialization is skipped].
switch(i) {
case a: ...
case b: ...
default: ...
}

is equivalent to:

if(i==a) goto label_a;
else if(i==b) goto label_b; //NB: i is not re-evaluated
else goto label_default;
{
label_a: ...
label_b: ...
label_default: ...
}

This can sometimes be abused to implement state machines
(as appears to be the case in the provided example) or
other nifty tricks (ever heard of Duff's device? see
http://en.wikipedia.org/wiki/Duff's_device).
But these special switch tricks are never a good idea,
except if the code is much messier without them...
[ as is the case for usage of goto ]
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Jul 10 '06 #2

Frederick Dean wrote:
Hi,guys!
I'm reading Stephen Dewhurst's book "C++ Gotchas",in gothca #7, I
meet a weird case:
bool Postorder::next() {
switch (pc)
case START:
while (true)
if (!child()) {
pc = LEAF;
return true;
case LEAF:
while (true)
if (sibling())
break;
else if (parent()) {
pc = INNER;
return true;
case INNER: ;
} else {
pc = DONE;
case DONE: return false;
}
}
}
I can't understand how the case goes, those cases are not even in the
same level, the author does not explain how the cases execute, could you
please help me with this? Any help is appreciated, thanks!
I think this code has nothing to do with goto.

please provide a minimal understandable code. If you can provide the
complete Postorder class, that's better or atleast tell

me if it is available online.

after my own indentation, I could write it as follows..

bool Postorder::next()
{
switch (pc)
case START:
while (true)
if (!child())
{
pc = LEAF;
return true;
case LEAF:
while (true)
if (sibling())
break;
else if (parent())
{
pc = INNER;
return true;
case INNER: ;
}
else
{
pc = DONE;
case DONE:
return false;
}
}
}

The above program has following flaws..

The program enters into switch only if the case is START.
There is no reason for putting a while(true) infinite loop.
infinit while loop breaks if and only if the return value of child()
function is false or 0.
the program cannot enter into case LEAF: statement at any case.
so starting from that point, the code is useless.

-- Murali Krishna.

Jul 11 '06 #3

Murali Krishna wrote:
Frederick Dean wrote:
Hi,guys!
I'm reading Stephen Dewhurst's book "C++ Gotchas",in gothca #7, I
meet a weird case:
bool Postorder::next() {
switch (pc)
case START:
while (true)
if (!child()) {
pc = LEAF;
return true;
case LEAF:
while (true)
if (sibling())
break;
else if (parent()) {
pc = INNER;
return true;
case INNER: ;
} else {
pc = DONE;
case DONE: return false;
}
}
}
I can't understand how the case goes, those cases are not even in the
same level, the author does not explain how the cases execute, could you
please help me with this? Any help is appreciated, thanks!

I think this code has nothing to do with goto.

please provide a minimal understandable code. If you can provide the
complete Postorder class, that's better or atleast tell

me if it is available online.

after my own indentation, I could write it as follows..

bool Postorder::next()
{
switch (pc)
case START:
while (true)
if (!child())
{
pc = LEAF;
return true;
case LEAF:
while (true)
if (sibling())
break;
else if (parent())
{
pc = INNER;
return true;
case INNER: ;
}
else
{
pc = DONE;
case DONE:
return false;
}
}
}

The above program has following flaws..

The program enters into switch only if the case is START.
There is no reason for putting a while(true) infinite loop.
infinit while loop breaks if and only if the return value of child()
function is false or 0.
the program cannot enter into case LEAF: statement at any case.
so starting from that point, the code is useless.

-- Murali Krishna.
I thought atleast one person will point out my errors. No one did.

I want to sincerely confess myself that my previous posting is not
correct.

I am writing this because some readers may consider that to be a
correct answer.

As Ivan said switch acts like a goto, it's correct. I never thought the
case statement can be written inside any block. Infact, I never wrote
in that way.

I think Stephen Dewhurst wanted to tell that there are people like me
who would confuse with switch-case statements. He shows how swith-case
can also be written.

OK, finally what I learnt is..

the case statement can be written any where even inside a while block.

in the above code, when the valid case is met, the control jumps to
that case statement.

Do I have to learn any thing more? Please let me know.

-- Murali Krishna

Jul 11 '06 #4
Frederick Dean wrote:
Hi,guys!
I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I
meet a weird case:
bool Postorder::next() {
switch (pc)
case START:
while (true)
if (!child()) {
pc = LEAF;
return true;
case LEAF:
while (true)
if (sibling())
break;
else if (parent()) {
pc = INNER;
return true;
case INNER: ;
} else {
pc = DONE;
case DONE: return false;
}
}
}
I can't understand how the case goes, those cases are not even in the
same level, the author does not explain how the cases execute, could you
please help me with this? Any help is appreciated, thanks!
Try googling for Duff's Device (it's even in the English wikipedia),
this exposes a very similiar situation of a loop in the middle of a
switch statement. There was a long debate whether Duff's Device was
legal C, and in the end it was agreed that it must be regarded as legal.
Of course, such optimizations are highly coupled and should be
documented pretty well (in the above case, I would expect an explanation
of about two times the number of code lines).

Regards,
Stuart
Jul 14 '06 #5

Ivan Vecerina wrote:
"Frederick Dean" <di*******@tom.comwrote in message
news:e8**********@news.cn99.com...
: Hi,guys!
: I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I
: meet a weird case:
[...]
: I can't understand how the case goes, those cases are not even in the
: same level, the author does not explain how the cases execute, could you
: please help me with this? Any help is appreciated, thanks!
[snip]
switch(i) {
case a: ...
case b: ...
default: ...
}

is equivalent to:

if(i==a) goto label_a;
else if(i==b) goto label_b; //NB: i is not re-evaluated
else goto label_default;
{
label_a: ...
label_b: ...
label_default: ...
}
since the switch is only a conditional jump, I would use goto in the
case above

many would not believe me, but goto can turn state-machines code
clearer

Jul 14 '06 #6

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

Similar topics

6
by: Christian Blackburn | last post by:
Hi Gang, I have so far been unable to get the TextStream Object to write on Fat32. I'm using Windows 2000 Pro Sp4. Scripts have write access and I even tried giving IUSR user level writes and...
0
by: Ruben Antonio Macias Doporto | last post by:
I have a ASP.NET app, and something weird is happening I have a xmldocument that has more than 10 nodes, I create an instance of a XMLNode and assign one node to it from the xmldocument. One line...
13
by: Dmitry Tkach | last post by:
Hi, everybody! Here is a weird problem, I ran into... I have two huge (80 million rows each) tables (a and b), with id as a PK on both of them and also an FK from b referencing a. When I try to...
11
by: ricolee99 | last post by:
Hi everyone, I'm trying to invoke my .exe application from a remote server. Here is the code: ManagementClass processClass = new ManagementClass ("\\\\" +"RemoteServerName" +...
5
by: _DG | last post by:
At least when you've got any docs in .CHM format in the folder tree. If you drop a file called CSharpDocs.chm into a folder: \toplevel\C#\docs, you can open it, but it won't display. The #...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
0
by: jaydog0910 | last post by:
Hi all I have some something weird with the HttpWebResponse and I was hoping someone could help me understand what is going on. This problem is already fixed but for my own curiosity I was...
2
by: TamaThps | last post by:
Hi, I'm using visual studio 2008 and normally when I get an error it shows what line it is on and which file etc. The error I'm getting I don't know how to solve or even what the problem is. This...
0
by: coolminded | last post by:
Dear all, when i run the following code it shows the error run-time error:'429' ActiveX Component cant create object i have also registered vbxmlrpc.dll what might the problem? thanx in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.