Nesting if statements in switch structures

K

Keith Thompson

Old Wolf said:
You can even do this:

switch(freq)
{
case 1:
if ( variable == 1 )
some_code();
else
case 2:
some_other_code();
}

Also possible is:

switch(freq)
{
case 1:
if ( variable == 1 )
{
some_code();
case 2:
some_other_code();
}
break;
}

Yes, you can. That doesn't mean you should (as I'm sure Old Wolf
knows, but others might not).

There's also Duff's Device; see <http://www.c-faq.com/misc/duff.html>.
Quoting from that page:

In his announcement of the technique to C's developers and the
world, Duff noted that C's switch syntax, in particular its ``fall
through'' behavior, had long been controversial, and that ``This
code forms some sort of argument in that debate, but I'm not sure
whether it's for or against.''
 
C

CBFalconer

Keith said:
Yes, you can. That doesn't mean you should (as I'm sure Old Wolf
knows, but others might not).

There's also Duff's Device; see <http://www.c-faq.com/misc/duff.html>.
Quoting from that page:

In his announcement of the technique to C's developers and the
world, Duff noted that C's switch syntax, in particular its ``fall
through'' behavior, had long been controversial, and that ``This
code forms some sort of argument in that debate, but I'm not sure
whether it's for or against.''

All this emphasizes my attitude that case labels are just further
labels, and should all be pulled out to the left margin, as in
(working from the above examples):

switch(freq) {
case 1: if ( variable == 1 )
some_code();
else
case 2: some_other_code();
}

Also possible is:

switch(freq) {
case 1: if ( variable == 1 ) {
some_code();
case 2: some_other_code();
}
break;
}

</stylewar>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
T

Technoid

Thanks for your comments on the question of nesting if statements in
switch structures!
I removed CASENAME and the code compiles now.
Thank you!!
 

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,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top