Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested case?

Status
Not open for further replies.

vintl

Technical User
Jun 9, 2000
72
0
0
MY
I want to run 2 case one after another, how can this be possible?
I've try to omit the break stmt after case 1: ; like this->

case '1' :
do_selection();
case '2' :
process_selected_item();
break;
default:
break;

but it didnt work out. hav other way in doing this?
thanks

 
I don't see what you're trying to achieve.

What you've shown is the equivalent of
Code:
if ( choice == '1' ) {
    do_selection();
    process_selected_item();
} else
if ( choice == '2' ) {
    process_selected_item();
} else {
}

With switch statements, you're limited to just omitting the break statement, to allow one case to 'fall through' into the next case. If the logic is more contrived than that, then you need to use if/else logic.
 
if you by 'nested case' means combining 2 cases in a switch statement by omitting the break between the 2, the answer is yes (At least I have done so a few times).

However I won't recommend it :

It's dangerous because you might overlook this later when/if adding more case statements.

Further you may spot the 'missing break' later and simply add the break - which will then cause the program to fail.

/JOlesen
 
Hi,

Try this

case '1' :
case '2' :
do_selection();
process_selected_item();
break;
default:
break;


Regards,
Amar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top