Switch/Case and operators

조회 수: 40 (최근 30일)
googo
googo 2013년 3월 25일
댓글: samaneh 2021년 5월 23일
I running this code and and I don't understand why it is writing me that x is less than y... can you help me figure this out?
>> x = 222, y = 3;
switch x
case x==y
disp('x and y are equal');
case x>y
disp('x is greater than y');
otherwise
disp('x is less than y');
end
x =x =
222
x is less than y
thank's.
  댓글 수: 3
Nitigya Joshi
Nitigya Joshi 2021년 3월 2일
ok
samaneh
samaneh 2021년 5월 23일
Dear Googo,
The resullt of Relational Operators is Boolean meaning it takes value 1 for true and 0 for false. Hence case can only be 0 or 1 in your code, which is not desired velue for your x. That's why "otherwise" and "disp('x is less than y');" will be executed for any value of your x and y.
The solution is that, you put x beside of your Relational Operators to keep the value of the x, such as bellow.
>> x = 222, y = 3;
switch x
case x*(x==y)
disp('x and y are equal');
case x*(x>y)
disp('x is greater than y');
otherwise
disp('x is less than y');
end
%%%%%% Result %%%%%
x =
222
x is greater than y

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 25일
편집: Azzi Abdelmalek 2013년 3월 25일
In your case you should use if/elseif
x = 222, y = 3;
if x==y
disp('x and y are equal');
elseif x>y
disp('x is greater than y');
else
disp('x is less than y');
end
  댓글 수: 2
googo
googo 2013년 3월 25일
Thanks for the comment! But it will be a little bit long because I need to check a specific date if the year is less then 1000 or 100 or 10 and put 3/2/1 zeros before respectivlly.. for example 1/1/800 will be 1/1/0800 same for the days and the month, if the month is less than 10... any way to shorten this out? if not... I guess I can write a number of if's but maybe there is another suggestion... thank you very much...
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 25일
This is not clear

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jan
Jan 2013년 3월 25일
Don't do this. Let sprintf care about leading zeros:
a = [1,2,800];
sprintf('%d/%d/%04d', a);

Benhur Tekeste
Benhur Tekeste 2019년 2월 14일
Hi, there.
From my point of view, the resullt of comparison operation is Boolean meaning it takes value 1 for true and 0 for false. Hence first the relational operation besides the case is evaluated and then compared to the expression besides the switch.
x = 222, y = 3;
switch x
case x==y % once the program runs, it will find that x is not equal to y meaning it is false and it
has value of zero. And zero is not equal to the value of x therefore it willnot
execute this body.
disp('x and y are equal');% same here too but x is greater than y it is true and has value of 1 but
1 is not equal to 222 therefore body in otherwise is executed
case x>y
disp('x is greater than y');
otherwise
disp('x is less than y');
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 2월 14일
change the switch x to switch true and then it will work.

댓글을 달려면 로그인하십시오.


thejas av
thejas av 2021년 5월 20일
편집: Walter Roberson 2021년 5월 20일
switch(m)
case 1
if(d>=1 && d<=19)
message = sprintf('\noptimistic, lovers of freedom, hilarious, fair-minded, honest and intellectual. \nThey are spontaneous and fun, usually with a lot of friends');
%data=text( message, 'FontSize',12, 'Color', [.6 .2 .6]);
text(-1800,800,message, 'FontSize',9, 'Color', [.6 .2 .6]);
else
message = sprintf('\nThey are ambitious, organized, practical, goal-oriented, and they do not mind the hustle.\n“They are ready to give up a lot in order to achieve that goal,” Verk says. \nThey also love making their own rules, which means they strive to reach high career positions.');
%data=text( message, 'FontSize',12, 'Color', [.6 .2 .6]);
text(-1800,800,message, 'FontSize',9, 'Color', [.6 .2 .6]);
end
case 2
if(d>=1 && d<=19)
message = sprintf('\nThey are ambitious, organized, practical, goal-oriented, and they do not mind the hustle.\n“They are ready to give up a lot in order to achieve that goal,” Verk says. \nThey also love making their own rules, which means they strive to reach high career positions.');
%data=text( message, 'FontSize',12, 'Color', [.6 .2 .6]);
text(-1800,800,message, 'FontSize',9, 'Color', [.6 .2 .6]);
else
message = sprintf('\n Progressive, original, independent, humanitarian\nRuns from emotional expression, temperamental, uncompromising, aloof\nFun with friends, helping others, fighting for causes, intellectual conversation, a good listener\n Limitations, broken promises, being lonely, dull or boring situations, people who disagree with them.');
%data=text( message, 'FontSize',12, 'Color', [.6 .2 .6]);
text(-1800,800,message, 'FontSize',9, 'Color', [.6 .2 .6]);
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 5월 20일
It is not clear how this is relevant to the Question ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resampling Techniques에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by