How do I end a program when a condition is met using only if-else-elseif statements?

6 ビュー (過去 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
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.
Sam Jenkins
Sam Jenkins 2019 年 9 月 9 日
Yes, sorry if that’s against the rules of the board. I just don’t understand the general concepts of nesting if-else statements and can’t seem to make it work.

サインインしてコメントする。

採用された回答

Rik
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 ExchangeLogical についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by