clarification of the use 'switch case'
6 次查看(过去 30 天)
显示 更早的评论
Hi,
I would just like a little clarification of the use of 'switch case', most of the examples I have looked at showed examples where only one of the 'case' are true. I have not found an explanation that makes it clear I can return any number of true or positive values.
My example, I have a number and want to ask 5 questions and execute code/function at all true values. All questions may be true, false or any combination in between.
switch 25
case >20
do a
case mod(3)
do b
case ==5^2
do c
end
Does the code execute every 'case' irrelevant of previous answer, if so do I use break or continue to exit the switch.
Regards,
AD
0 个评论
回答(1 个)
Amith Kamath
2011-12-4
In MATLAB switch blocks, only the first matching case executes:
result = 52;
switch(result)
case 52
disp('result is 52')
case {52, 78}
disp('result is 52 or 78')
end
This code returns
result is 52
Hope this clarifies your questions!
3 个评论
Amith Kamath
2011-12-4
I wonder why that's not so obvious, considering the number of such questions here!
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!