473,464 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nesting if statements in switch structures

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
22 2203
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


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

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
remove CASENAME

Apr 4 '06 #6
"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
"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
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
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
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
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
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
"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

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
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
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
"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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Philip WATTS | last post by:
I am trying to carry out multiple checks on some input data. I am doing this by the running the data through a number of functions. i.e. I have an onclick that calls a function, which in turn calls...
5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
1
by: kus | last post by:
i was writing a program for the application of stacks..in which i had to use nested structures.....precisely speaking it was like this... struct car { int license,s,e; } typedef struct...
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
6
by: stephen.cunliffe | last post by:
Hi, I'm looking for opinion/facts/arguments on the correct nesting of UL, OL, & LI elements. For example, this is what I want (unordered list): * Item 1 * Item 2 * Item 3
25
by: bweaverusenet | last post by:
Hi. I am trying to get some javascript to work in IE6, from the address line. It works in Firefox and, I believe, IE7. It appears that with some number of nested structures, the code does not...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.