By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Nesting if statements in switch structures

P: n/a
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
Apr 4 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes.
switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:

The compiler says we have a syntax error on the if statement.


I don't know what CASENAME is supposed to be but it is a syntax error,
everything else looks okay, conceptionally. It would much more helpful
if you provided the exact code that caused the error and the exact
error message, why wouldn't you do this?

Robert Gamble

Apr 4 '06 #2

P: n/a


Technoid wrote On 04/04/06 15:19,:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes.
switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.


Get rid of `CASENAME', for starters. Then
replace `do some code' and `do some other code'
with valid code.

--
Er*********@sun.com

Apr 4 '06 #3

P: n/a
Technoid schrieb:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes.
switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.


This nicely illustrates why it makes sense to provide
a compiling minimal example. If the above is copied from
your code, then remove CASENAME.
Apart from that consider

#include <stdio.h>

enum A_VAL { SOME_VAL, SOME_OTHER_VAL, TEN_VAL = 10 };

#define MAGIC 42

int main (void)
{
int a;
double b;

a = TEN_VAL;
b = 7.5;

switch (a) {
case SOME_VAL:
case SOME_OTHER_VAL:
puts("AllowedVal");
break;
case TEN_VAL:
if (b * MAGIC >= a + MAGIC) {
puts("TenMagic");
} else {
puts("TenMundane");
}
break;
default:
puts("Default");
break;
}

return 0;
}

Compiles and links.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 4 '06 #4

P: n/a

Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Only on the condition that you unconditionally know how to nest
conditions.

switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.


Apr 4 '06 #5

P: n/a
remove CASENAME

Apr 4 '06 #6

P: n/a
"Ivanna Pee" <fr***@Infectedmail.com> writes:
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?


Only on the condition that you unconditionally know how to nest
conditions.

[...]

You've posted four responses in this newsgroup. One of them was
actually meaningful; the others have been completely useless, and not
nearly as funny as you think.

If you want to participate here, feel free to do so. If not, please
go away.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 4 '06 #7

P: n/a
"blufox" <pr*************@gmail.com> writes:
remove CASENAME


From what?

Read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 4 '06 #8

P: n/a
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

Yes - but I'd suggest you don't, call an appropriately named function
instead. I makes the code easier to read and maintain.

--
Ian Collins.
Apr 4 '06 #9

P: n/a
On Wed, 05 Apr 2006 09:12:28 +1200, Ian Collins <ia******@hotmail.com>
wrote:
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

Yes - but I'd suggest you don't, call an appropriately named function
instead. I makes the code easier to read and maintain.


Depends. Often I'd rather have a simple conditional in front of me
than buried in a function somewhere else.

--
Al Balmer
Sun City, AZ
Apr 4 '06 #10

P: n/a
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.


It's possible (with the correction noted by others), but IMO it's
a VERY poor idea. Whenever you nest decision structures it means
you haven't thought the problem through very well. Worse, you are
asking for trouble because the resulting code is harder to read
and MUCH harder to debug. The usual problem is accidentally putting
in logical conditions that are either impossible to realize or two
or more conditions that are not mutually exclusive and can lead to
unexpected behavior. The first creates dead code that may not be
positively harmful, but is certainly unprofessional. The second can
produce bugs that are sporadic and therefore hard to track down. I
know, because I have committed both errors in my time.

In C, using switch, you can easily generate a finite state machine.
Table-based FSM's are clear to understand and handle most logic
problems without resorting to nested decision structures. Sad
experience leads me to recommend the FSM approach to complex logic.

--
Julian V. Noble
Professor Emeritus of Physics

http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the
toothache patiently."

-- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
Apr 4 '06 #11

P: n/a
Al Balmier wrote:
On Wed, 05 Apr 2006 09:12:28 +1200, Ian Collins <ia******@hotmail.com>
wrote:

Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?


Yes - but I'd suggest you don't, call an appropriately named function
instead. I makes the code easier to read and maintain.

Depends. Often I'd rather have a simple conditional in front of me
than buried in a function somewhere else.

Simple is the key word there!

--
Ian Collins.
Apr 4 '06 #12

P: n/a
On Wed, 05 Apr 2006 11:05:34 +1200, Ian Collins <ia******@hotmail.com>
wrote:
Al Balmier wrote:
On Wed, 05 Apr 2006 09:12:28 +1200, Ian Collins <ia******@hotmail.com>
wrote:

Technoid wrote:

Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes - but I'd suggest you don't, call an appropriately named function
instead. I makes the code easier to read and maintain.

Depends. Often I'd rather have a simple conditional in front of me
than buried in a function somewhere else.

Simple is the key word there!


Agreed. Julian's point is well taken, also. If you find that you need
conditionals in a switch, consider the possibility that there's a
better way to approach the problem.

--
Al Balmer
Sun City, AZ
Apr 5 '06 #13

P: n/a
"Julian V. Noble" <jv*@virginia.edu> wrote:
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
It's possible (with the correction noted by others), but IMO it's
a VERY poor idea. Whenever you nest decision structures it means
you haven't thought the problem through very well.


Fiddlesticks. To illustrate, here's a paraphrase of code I wrote
recently, in another language:

switch (object.type) {
case TextFrame:
if (adjust_content)
fit_header_width(object);

fit_frame_content(object);
object.length+=descender;
break;

case ImageFrame:
if (adjust_content) {
fit_image_frame_minimal(object);
if (object.content.width<object.width)
fit_image_frame_maximal();
}
fit_frame_content(object);
break;

default:
message("Can't fit that.");
break;
}

where adjust_content is a (user-provided) parameter to the fitting
procedure. Care to explain how I'd have done this more clearly with a
single switch?

Richard
Apr 5 '06 #14

P: n/a

Keith Thompson wrote:
"Ivanna Pee" <fr***@Infectedmail.com> writes:
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?


Only on the condition that you unconditionally know how to nest
conditions.

[...]

You've posted four responses in this newsgroup. One of them was
actually meaningful; the others have been completely useless, and not
nearly as funny as you think.

If you want to participate here, feel free to do so. If not, please
go away.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


OK. Alright I'll let you do all of the homework assignments posted
here. When I was learning C, 17+ years ago, we learned by reading
manuals, trial and error, instrumenting code and debugging. Suffering
over bugs, then eventually sovling them. That is how you learn. You
learn because you know that you never want to make that mistake again,
and so it sinks in fast, real fast. You think you are helping these
kids, but you are not. They need to learn how to learn, not have a
teacher over their shoulder pointing out bugs.

If you want to participate here, throw hints/suggestions at the OPs
design / coding problem. Do not give specifics, as I see you frequently
do, unless of course the question is a specific C question.

Apr 5 '06 #15

P: n/a
Richard Bos wrote:
"Julian V. Noble" <jv*@virginia.edu> wrote:
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

It's possible (with the correction noted by others), but IMO it's
a VERY poor idea. Whenever you nest decision structures it means
you haven't thought the problem through very well.


Fiddlesticks. To illustrate, here's a paraphrase of code I wrote
recently, in another language:

switch (object.type) {
case TextFrame:
if (adjust_content)
fit_header_width(object);

fit_frame_content(object);
object.length+=descender;
break;

case ImageFrame:
if (adjust_content) {
fit_image_frame_minimal(object);
if (object.content.width<object.width)
fit_image_frame_maximal();
}
fit_frame_content(object);
break;

default:
message("Can't fit that.");
break;
}

where adjust_content is a (user-provided) parameter to the fitting
procedure. Care to explain how I'd have done this more clearly with a
single switch?

Richard


To every sad experience there are exceptions. Your example is simple
enough that it is still clear, although it might have been clearer had
the actions for each case been factored out as separate subroutines
with names that indicate what they do, i.e.

switch (object.type) {

case TextFrame:
fitText(adjust_content,object);
break;

case ImageFrame:
fitImage(adjust_content,object);
break;

default:
message("Can't fit that.");
break;
}

where the if's are factored into the subroutines. If your language
permits ? marks in names you could indicate the conditionality by
naming the routines ?fitText and ?fitImage , and it would be even
clearer. But these are stylistic decisions and "de gustibus non
disputandum est", after all.

How bad it gets depends on how deeply it is nested. Moreover, there
are other useful decision structures, such as decision tables. One
is not limited to FSM's, of course.

--
Julian V. Noble
Professor Emeritus of Physics

http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the
toothache patiently."

-- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
Apr 5 '06 #16

P: n/a
On Wed, 05 Apr 2006 12:34:29 -0400, "Julian V. Noble"
<jv*@virginia.edu> wrote:
Richard Bos wrote:
"Julian V. Noble" <jv*@virginia.edu> wrote:
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
It's possible (with the correction noted by others), but IMO it's
a VERY poor idea. Whenever you nest decision structures it means
you haven't thought the problem through very well.


Fiddlesticks. To illustrate, here's a paraphrase of code I wrote
recently, in another language:

switch (object.type) {
case TextFrame:
if (adjust_content)
fit_header_width(object);

fit_frame_content(object);
object.length+=descender;
break;

case ImageFrame:
if (adjust_content) {
fit_image_frame_minimal(object);
if (object.content.width<object.width)
fit_image_frame_maximal();
}
fit_frame_content(object);
break;

default:
message("Can't fit that.");
break;
}

where adjust_content is a (user-provided) parameter to the fitting
procedure. Care to explain how I'd have done this more clearly with a
single switch?

Richard


To every sad experience there are exceptions. Your example is simple
enough that it is still clear, although it might have been clearer had
the actions for each case been factored out as separate subroutines
with names that indicate what they do, i.e.


As you say, it's in the eye of the beholder, but I find Richard's
version clearer than yours. It tells me immediately what
"adjust_content" is good for. In your version, I would be surprised to
go to fitText() only to find out that the passed parameter was used
only to return without doing anything. In fact, I would factor out
that useless parameter :-)
switch (object.type) {

case TextFrame:
fitText(adjust_content,object);
break;

case ImageFrame:
fitImage(adjust_content,object);
break;

default:
message("Can't fit that.");
break;
}

where the if's are factored into the subroutines. If your language
permits ? marks in names you could indicate the conditionality by
naming the routines ?fitText and ?fitImage , and it would be even
clearer. But these are stylistic decisions and "de gustibus non
disputandum est", after all.

How bad it gets depends on how deeply it is nested. Moreover, there
are other useful decision structures, such as decision tables. One
is not limited to FSM's, of course.


--
Al Balmer
Sun City, AZ
Apr 5 '06 #17

P: n/a
"Ivanna Pee" <fr***@Infectedmail.com> writes:
[...]
If you want to participate here, throw hints/suggestions at the OPs
design / coding problem. Do not give specifics, as I see you frequently
do, unless of course the question is a specific C question.


Sadly, you've chosen to introduce yourself to this newsgroup by acting
like a particularly obnoxious troll. It's not inconceivable that you
might have a valid point, but you've already destroyed your chance of
being taken seriously. Calling yourself "Ivanna Pee" doesn't help
either. That's life.

You're not the first person to jump in here and tell us we've been
running this newsgroup wrong for all these years.

BTW, it's rarely necessary to quote the entire article to which you're
replying; in particular, don't quote the signature unless you're
actually commenting on it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 5 '06 #18

P: n/a
Ian Collins wrote:
Al Balmier wrote:
On Wed, 05 Apr 2006 09:12:28 +1200, Ian Collins <ia******@hotmail.com>
wrote:

Technoid wrote:

Is it possible to have a conditional if structure nested inside a
conditional switch structure?

Yes - but I'd suggest you don't, call an appropriately named function
instead. I makes the code easier to read and maintain.


Depends. Often I'd rather have a simple conditional in front of me
than buried in a function somewhere else.

Simple is the key word there!

Indeed.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 5 '06 #19

P: n/a
Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:

The compiler says we have a syntax error on the if statement.


That's caused by CASENAME.

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;
}

Apr 6 '06 #20

P: n/a
"Old Wolf" <ol*****@inspire.net.nz> writes:
[...]
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.''

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 6 '06 #21

P: n/a
Keith Thompson wrote:
"Old Wolf" <ol*****@inspire.net.nz> writes:
[...]
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.''


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/>
Apr 6 '06 #22

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

Technoid wrote:
Is it possible to have a conditional if structure nested inside a
conditional switch structure?

switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.


Apr 6 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.