How do I end a program when a condition is met using only if-else-elseif statements?
10 次查看(过去 30 天)
显示 更早的评论
I’m making a program where I need to have the program stop asking questions if an invalid input is entered by the user. How would I do this using only if-elseif-else statements, not using commands like return or break?
2 个评论
Rik
2019-9-9
Why are you limiting yourself? Is this homework? Using keywords like return and break will make code much more readable and compact.
采纳的回答
Rik
2019-9-9
编辑:Rik
2019-9-9
Something like this will work (edit to add nesting):
function out=MyFunction(in)
if in<1
if in==0.5
%do something
else
%do something different
end
elseif in==2
%do something else
else
%do nothing
end
%don't put code here
end
You could also use code at the beginning of your function to find out the type of code that should be run and then use that in a switch case structure.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!