Switch Loop with Boolean expressions? Can it be done?

3 ビュー (過去 30 日間)
Mau Dudas
Mau Dudas 2019 年 10 月 27 日
コメント済み: Guillaume 2019 年 10 月 28 日
So I am trying to get a switch loop to display values of x=rand(1,10) when the values are greater than and smaller than 0.6?
Been trying with this however it keeps either telling me that my x value needs to be a scalar or character variable or just the regular unexpected error message.
switch (x)=true
case (x>0.6)
disp 'Large value in x found'
otherwise (x<0.6)
disp 'Small value in x found'
end
I know something else would be probably more pragmatic here but it's homwork so I do not make the rules...
Thanks in advance!

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 10 月 27 日
switch true
with no variable..
Also the otherwise should not have a condition
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 28 日
Yes.
I do not recommand this use of switch() but it is legal. It would almost always be clearer to use if/elseif trees instead of switch true
Guillaume
Guillaume 2019 年 10 月 28 日
As Walter said, yes you can do this.
As Walter also recommended, you shouldn't do it. The majority of readers (particularly people familiar with C-like languages) would expect a constant in the case expression and a non-constant expression for the switch. You're going to confuse a lot of people if you use that syntax.
If you do use it despite our recommendation, then a comment in the code explaining in details why it is used (as opposed to the simpler and clearer if...else) would be extremely recommended.

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

カテゴリ

Help Center および File ExchangeC2000 Microcontroller Blockset についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by