How to use If else statements?

8 ビュー (過去 30 日間)
Danny Maefengea
Danny Maefengea 2020 年 6 月 23 日
コメント済み: Jiahao CHANG 2020 年 6 月 23 日
Hi everyone, I have tried programming in Matlab to check if a number is in the range of 10-20 (use if-else), but I couldn't get the exected results.
Can I get help please?
Thank you in advance for your help.

採用された回答

Jiahao CHANG
Jiahao CHANG 2020 年 6 月 23 日
編集済み: Jiahao CHANG 2020 年 6 月 23 日
Hi Danny,
For using if-else function, I recommend you to type <help if> in matlab command line or this link if, elseif, else to see the documentation of how to use help function.
I will show you an example to check if a number is in 10-20. But Im not sure whether it is want you want to see unless you could show us more details about your script.
i = input('please enter a number:');
if i<=20&&i>=10
disp('this number is in the range of 10-20');
else
disp('this number is NOT in the range of 10-20');
end
  2 件のコメント
Danny Maefengea
Danny Maefengea 2020 年 6 月 23 日
Thank you sir for your help.
Jiahao CHANG
Jiahao CHANG 2020 年 6 月 23 日
You're welcome! If you have any other confusing things don't hesitate to ask me ;)

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

その他の回答 (1 件)

Danny Maefengea
Danny Maefengea 2020 年 6 月 23 日
Consider the following code fragment:
forx = 1 : 5
fory = 1 : 3
disp('LOVE MELB');
end
disp('LOVE VU');
  • How many times will the phrase ‘LOVE MELB’ be printed? 15 times.What are the corresponding x and y values during each iteration? The corresponding x values are 5, corresponding y values are 3.
  • How many times will the phrase ‘LOVE VU’ be printed? 5 times .What are the corresponding x and y values during each iteration? Corresponding x values are 3, corresponding y values are 5
  2 件のコメント
Danny Maefengea
Danny Maefengea 2020 年 6 月 23 日
sir I have this question and written in bold are my answers. can u please check them and see if they are correct?
Thank you so much for your help
Jiahao CHANG
Jiahao CHANG 2020 年 6 月 23 日
<‘LOVE MELB’ be printed? 15 times> and <‘LOVE VU’ be printed? 5 times> are right obviously.
For each interation about 'LOVE MELB', the x and y value are different. For each x from 1 to 5, y equals to 1,2 and 3.
For each interation about 'LOVE VU', the x value are different. x varie from 1 to 5, but y always equals to 3.
It's not very clear to explain this by words. I suggest you to set breakpoints on line 3 then see how Matlab program by seeing how the x and y value change in the workspace.
for x = 1 : 5
for y = 1 : 3
disp('LOVE MELB');
end
disp('LOVE VU');
end

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

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by