coldfusion calling another case within a switch - coldfusion

I have a switch like thus (written in cfscript):
switch (something) {
case "stuff":
if(this eq that){
writeDump("hello");
} else { /* do other? */ }
break;
case "other":
//do something else
break;
}
In my else, I want to be able to tell it that I want the "other" case to be invoked. Is this possible? (I seem to remember doing this in other languages.)

There is no GOTO construct in CF, no. And that's pretty much what you're asking for.
If your switch is really as simple as you indicate, and you want to fall through to the NEXT case when the condition is false, what you could do is to have the break statement in the true branch of the if clause, and have no break statement in the false branch. Then when the false branch runs, processing will not exit the case when it's done; it'll fall through to the next case.

As Adam explained, it is possible, here is some code to show how:
something = "stuff";
test = "more stuff";
switch (something) {
case "stuff":
if(test eq "more stuff"){
writeOutput("<p>Something</p>");
break;
}
case "other":
writeOutput("<p>something Else</p>");
break;
}
Change the values of 'something' and 'test' to see different results. When using a switch in cfscript, if there is no 'break' then ColdFusion will continue processing the cases until it hits one.

As far as I'm aware that's not possible. You'll need to use an if else statement, or nested is/else, instead of the switch.
something = "other";
if (something eq "stuff" and 1 eq 2) {
writeDump("hello");
} else if (something eq "other") {
writedump("other");
}

Related

How to replace the 'break'?

In my program I have a structure similar to the following one:
while (true) {
int c;
cout << "Type a command: ";
cin >> command;
switch (c) {
case 1:
// [...]
if (!condition) break;
// [...]
if (!condition2) break;
// [...]
if (!condition3) break;
// [...]
break;
case 2:
// [...]
break;
default:
break;
}
}
But our professor told us to never use the break except in the switch case to exit... I was wondering if there's a better and smarter way to replace the block if(!condition) break;.
My main goal is to prevent the program from doing certain actions if condition is not verified.
In order to avoid break you need to use the opposite condition, and instead of breaking the flow, control it:
switch (c) {
case 1:
// [...]
if (condition) {
// [...]
if (condition2) {
// [...]
if (condition3) {
// [...]
} // if the third condition is false, you will continue to the break.
} // if the second condition is false, you will continue to the break.
} // if the first condition is false, you will continue to the break.
break;
// ...
}
EDIT
To avoid complex conditions, you can use functions:
void condition_1_actions();
void condition_2_actions();
void condition_3_actions();
// ... main ...
case 1:
if (condition) condition_1_actions();
break;
// ... After main ...
condition_1_actions() {
// do some actions
// Calculate condition2 or pass it as parameter
if (condition2) condition_2_actions();
}
condition_2_actions() {
// do some actions
// Calculate condition3 or pass it as parameter
if (condition3) condition_3_actions();
}
condition_3_actions() {
// do some actions
}
I'm sure that "your professor" would agree: "if it ain't broke, don't fix it."
The logic as-written is clear, and I presume that it works. Also, as-written it would be maintainable: I wouldn't have to radically change the source-code if when I needed to add a new condition to it. Just leave it alone. Anything that you hear from a professor – or anyone else – is a guideline!
"Clarity" is always key, because as years go by your source-code is going to be added-to by many others. I like the original idiom because each of the cases is clearly distinct: "make this test, and then bail-out." (Could be break or return depending on the situation.) It's very easy to see how the logic goes, and it's equally easy to add another case to it "in the same vein."
In the end – "it makes absolutely no difference to the digital computer." (As Perl programmers like to say, "there's more than one way to do it.™") But it might be attractive to your [future ...] colleagues. There are no absolutes here. Just try to be nice to them. Be "stupid-obvious."

Using a switch in a do..while loop, in C++

A simple programm that reads strings, and responds using a switch;
in this do-while loop containing a switch, I am able to run case 1-4 with no issues, but once i hit the default case, the programme simply loops the default case over and over again the code is as follows;
do { switch ( switchstring (entry, input) )
/*the switchstring function is one 1 wrote to convert a given entry(string),
into an input(integer)*/
{
case 1:
//code
repeat = 2;
break;
case 2:
//code
repeat = 2;
break;
case 3:
//code
repeat = 2;
break;
case 4:
//code
repeat = 2;
break;
default:
//code
repeat = 1;
break;}} while(repeat == 1);
the 2nd question is regarding my switchstring() function; is there a way to change the switch function such that it reads;
case (insert string):
i.e. so that I can remove the entire switchstring() function
thanks in advance!
Show us how switchstring (entry, input) works.
The problem you are facing is because, in default you do the following:
repeat = 1;
Which makes while(repeat == 1) always true. And then switchstring (entry, input) always return something that makes your switch block always go the the default case again.
When no case will be true in switch, then it will go in default case of switch and you are specifying repeat=1; in default. After that while condition will be checked and it will be true because repeat is 1, again it will go to do and check condition, your switch function will return something and it will go to default.
To solve 2nd question regarding your switchstring() function, you have to show your code what you are doing in that function, So that i can give you best suggestion.

While loop in case statement (C++)

Is anyone can help me resolve I think trivial issue for someone who knows C langue quite well.
I have some piece of code however label_start_menu: doesn't work for me.
What I want to achieve is back to default: Now if e.g. 3 == 3 while loop is working infinite and is not possible to return to default:
switch (counter1) {
case 1:
menu_clock();
delay(2000);
while(true){
Serial.println("loop1");
Serial.println("loop1");
Serial.println("loop1");
if(3 == 3)
{
break;
goto label_start_menu;
}
}
break;
case 2:
menu_media();
delay(2000);
break;
default:
label_start_menu:
menu_start();
break;
}
}
Thanks,
ojdas
if(3 == 3)
{
break;
goto label_start_menu;
}
goto will never get executed because the break statement will break out of your loop right there.
My advice would be to get rid of the goto, and to just call menu_start() in the if statement, and then break, instead of using a goto.
I'm not sure what you're trying to do with your while loop, exactly, but you've got your break; statement in front of your goto label_start_menu; statement, so it will never be reached.
More complete answer: there's absolutely no need for the goto in the first place. You're already calling another function. Rewrite the whole thing like this:
switch (counter1)
{
case 1:
menu_clock();
delay(2000);
Serial.println("loop1");
Serial.println("loop1");
Serial.println("loop1");
menu_start();
break;
case 2:
menu_media();
delay(2000);
break;
default:
menu_start();
break;
}
the break should be the last thing..
also, why put it in a loop? that is an infinite loop.. just call it and then break breaking will get you out of the switch and will not continue so make sure you do everything before you break, so put your goto label_start_menu; before the break and remove the if statement and the other break under it.
if you want to loop it then there are other loop methods you can use.. google is great with that!
Im going to repost #40two's answer, because it exactly hits the nail about usage of goto (besides the other flaws coming in the same way (no reasonable loop break conditions)).
#40two: XKCD is a real good source for insight, as lomg you're able to read it!

Execute all cases in switch statement - specific issue

How to execute all cases in switch statement without duplicating existing in big "all-in-one" case ?
For e.g.
switch(obj.nObjType)
{
case eElephant:
...
break;
case eFoo:
...
break;
case eSeptember:
...
break;
default:
return;
}
I want force executing code for all 3 cases (eElephant, eFoo, eSeptember) like there is no break;, for example in case of nObjType = eAllTypes .
You can't just ignore a break. Rather, restructure your code.
switch(obj.nObjType) {
case eElephant:
pokeElephant();
break;
case eFoo:
barTheFoo();
break;
case eSeptember:
rememberSeptember();
break;
case eAllTypes:
pokeElephant();
barTheFoo();
rememberSeptember();
break;
default:
return;
}
Then use if statements:
if (obj.nObjType == eElephant || obj.nObjType == eAllTypes)
{
// Elephant code
}
if (obj.nObjType == eFoo || obj.nObjType == eAllTypes)
{
// Foo code
}
// etc.
This answer is how it should be done.
However:
I can see, it's hard to find as right solution without generating lots
of code lines. Thanks #TobiMcNamobi, but in my switch there is about
200 of cases, so creating a function for each case is not a good idea.
I think, the best can be (unfortunately) duplicating all cases in a
big one. Because, I suppose, if else is much less efficient then
switch statement ?
With this many cases (what on earth are you working on?) even extracting methods leaves you with 200 extra function calls in the body of your switch statement. This could get harder to maintain quickly.
In this case, I would do the following:
switch(enum_variable)
{
case e_all:
case e_1:
e_1_function();
if(enum_variable != e_all){break;} //**
case e_2:
e_2_function();
if(enum_variable != e_all){break;} //**
//...
default:
break;
}
The lines marked //** will break when not doing all cases. It is quick to implement (can be done with a search and replace of break; which speeds things up for you) and does what you want.
With that said, it's still a pretty terrible way of doing it, but exceptions do sometimes have to be made.
I do however recommend that you do not turn to this as a style to use in the future. You may find that you can do better by re-thinking the workflow of your project, as this will probably allow you to simplify and improve. Of course, we don't know what you are working on, so I can't offer more advice.
If you put your switch statement in a function, and make your obj have a list of flags rather than a flag, you can call the function with the switch statement for each flag in the list. Pseudo:
void dostuff(flag)
switch(flag) {
// ...
}
}
for (auto flag in obj.flags) {
dostuff(flag)
}

Switch Statement continue

Is the following possible in C++?
switch (value) {
case 0:
// code statements
break;
case 1:
case 2:
// code statements for case 1 and case 2
/**insert statement other than break here
that makes the switch statement continue
evaluating case statements rather than
exit the switch**/
case 2:
// code statements specific for case 2
break;
}
I want to know if there is a way to make the switch statement continue evaluating the rest of the cases even after it has hit a matching case. (such as a continue statement in other languages)
How about a simple if?
switch (value)
{
case 0:
// ...
break;
case 1:
case 2:
// common code
if (value == 2)
{
// code specific to "2"
}
break;
case 3:
// ...
}
Once the case label is decided, there is no way to have the switch continue to search for other matching labels. You can continue to process the code for the following label(s) but this doesn't distinguish between the different reasons why a case label was reached. So, no, there is no way to coninue the selection. In fact, duplicate case labels are prohibited in C++.
Yep, just don't put in a break. It will naturally fall down to the other switch statements.