If inside the switch chassis to limit the cases
thanks for answers.
I would like to know if it is possible to limit some cases of the swtich-case statement using conditional expressions. Like the code is next.
switch(a)
{
case 1:
{
do_1();
break;
}
case 2:
{
do_2();
break;
}
if(condition)
{
case 3:
{
do_3();
break;
}
break;
}
}
Edited, sorry guys, I got the wrong state, it is not related to the toggle variable at all. Just another condition, external. I just want to know if I can restrict the cases to external conditions, otherwise, the cases inside the IF will not be parsed if the condition is not met.
Do I need a second break inside the if?
source to share
Or check the condition in each case:
switch(a)
{
case 1:
{
do_1();
break;
}
case 2:
{
do_2();
break;
}
case 3:
{
if (condition)
do_3();
break;
}
case 4:
{
if (condition)
do_4();
break;
}
}
Or use a block default
with a second one switch
:
switch(a)
{
case 1:
{
do_1();
break;
}
case 2:
{
do_2();
break;
}
default:
{
if (condition)
{
switch (a)
{
case 3:
{
do_3();
break;
}
case 4:
{
do_4();
break;
}
}
}
break;
}
}
source to share
C ++ doesn't support this. But you can use function calls to split the switch into pieces to reduce code duplication. If you tried to do something along these lines:
void func()
{
switch(a)
{
case 1:
{
do_1();
break;
}
case 2:
{
do_2();
break;
}
if(condition)
{
case 3:
{
do_3();
break;
}
case 4:
{
do_3();
break;
}
case 5:
{
do_3();
break;
}
break;
}
}
}
You can break it down like this:
void func()
{
if(!func_a(a) && condition)
func_b(a);
}
bool func_a(a)
{
switch(a)
{
case 1:
{
do_1();
return 1;
}
case 2:
{
do_2();
return 1;
}
default: return 0;
}
}
void func_b(a)
{
switch(a)
{
case 3:
{
do_3();
break;
}
case 4:
{
do_3();
break;
}
case 5:
{
do_3();
break;
}
}
}
source to share
Compiles to C ++. But the result was unexpected for me .
#include <iostream>
#include <string>
int test(int action, bool allow_restricted)
{
int success = -1;
switch(action)
{
case 1:
std::cout << "Performing " << action << std::endl;
success = 0;
break;
case 2:
std::cout << "Performing " << action << std::endl;
success = 0;
break;
if(allow_restricted)
{
case 3:
std::cout << "Performing restricted " << action << std::endl;
success = 0;
break;
case 4:
std::cout << "Performing restricted " << action << std::endl;
success = 0;
break;
}
default:
break;
}
return success;
}
int main()
{
test(1,false);
test(3,false);
test(3,true);
}
This is not the conclusion we want.
Performing 1
Performing restricted 3
Performing restricted 3
This does not work as expected because the statement switch-case
proceeds instead of executing the statement if
.
But in the case the allow_restricted == false
operator if
works (not when action == 3
and allow_restricted == false
).
int test_fallthrough(int action, bool allow_restricted)
{
int success = -1;
switch(action)
{
case 1:
std::cout << "fallthrough " << 1 << std::endl;
success = 0;
case 2:
std::cout << "fallthrough " << 2 << std::endl;
success = 0;
if(allow_restricted)
{
case 3:
std::cout << "fallthrough restricted " << 3 << std::endl;
success = 0;
case 4:
std::cout << "fallthrough restricted " << 4 << std::endl;
success = 0;
}
default:
break;
}
return success;
}
int main()
{
test_fallthrough(1,false);
test_fallthrough(1,true);
}
Output.
fallthrough 1
fallthrough 2
fallthrough 1
fallthrough 2
fallthrough restricted 3
fallthrough restricted 4
With the while
loops inside the switch, it gets more interesting.
void test_fun_fallthrough(int action, bool allow_restricted)
{
bool initialize = false;
switch(action)
{
do
{
case 1:
std::cout << "Fun fallthrough, initializing " << 1 << std::endl;
initialize = false;
case 2:
std::cout << "Fun fallthrough " << 2 << std::endl;
if(allow_restricted)
{
case 3:
std::cout << "Fun fallthrough restricted " << 3 << std::endl;
if(3 == action)
{
initialize = true;
action = 1;
}
case 4:
std::cout << "Fun fallthrough restricted " << 4 << std::endl;
}
} while(initialize);
default:
break;
}
}
int main()
{
test_fun_fallthrough(3,true);
std::cout << "*********************" << std::endl;
test_fun_fallthrough(3,false);
}
Fun fallthrough restricted 3
Fun fallthrough restricted 4
Fun fallthrough, initializing 1
Fun fallthrough 2
Fun fallthrough restricted 3
Fun fallthrough restricted 4
*********************
Fun fallthrough restricted 3
Fun fallthrough restricted 4
Fun fallthrough, initializing 1
Fun fallthrough 2
Suddenly, we can jump to restricted code even when allow_restricted
it matters false
.
source to share