Using switch to identify even or dd

조회 수: 266 (최근 30일)
cgo
cgo 2022년 7월 20일
댓글: Walter Roberson 2022년 7월 20일
Problem in brief: Print “Odd” if the argument is 1, 3, or 5, “Even” if the argument is 0, 2, or 4, and “Let me get back to you on that one.” for any other value.
function [y] = even_odd(x)
r = mod(x,2);
switch x
case x<=4 && r ==0
fprintf('even \n')
case x<=5 && r == 1
fprintf('odd \n')
otherwise
fprintf('i will get back to you on that \n')
end
end
I dont understand why this function doesn't give the correct response. Please provide insights.
  댓글 수: 2
MJFcoNaN
MJFcoNaN 2022년 7월 20일
Hello,
I will suggest you learn the basic programma of Matlab firstly. There may be significant discrepancy between languages.
cgo
cgo 2022년 7월 20일
this is matlab language. I want to know if there is a mistake in the logic as I am unable to get the intended outputs.

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 7월 20일
편집: Walter Roberson 2022년 7월 20일
even_odd(-88)
even
even_odd(2)
even
even_odd(3)
odd
even_odd(7)
i will get back to you on that
function [y] = even_odd(x)
r = mod(x,2);
switch true
case x<=4 && r ==0
fprintf('even \n')
case x<=5 && r == 1
fprintf('odd \n')
otherwise
fprintf('i will get back to you on that \n')
end
end
  댓글 수: 2
cgo
cgo 2022년 7월 20일
i am unaware of the 'true' statement there. why does it make the code work? Thanks for your insights.
Walter Roberson
Walter Roberson 2022년 7월 20일
The value that you list in the switch statement is compared to the value listed in the case. The values in your case are things like x<=4 && r==0 which is a logical expression, so the values in your case are either true or false so you need to switch on one of true, false, 0, or 1 . You want to select the case that is true, so you have to switch on true
... in practice you do not need that comparison for x inside the switch. You might hypothetically want to test against
fprintf('%ld\n', flintmax)
9007199254740992
which is the largest double precision integer that can be reliably tested for division by 2.

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

추가 답변 (2개)

KSSV
KSSV 2022년 7월 20일
You have only two options x can be either even or odd..that's all.
x = 4 ;
r = mod(x,2);
if r == 0
fprintf('%d is Even\n',x)
else
fprintf('%d is Odd\n',x)
end
  댓글 수: 2
cgo
cgo 2022년 7월 20일
sorry, the problem insists on using switch. I am practising the command since I know if-else already.
KSSV
KSSV 2022년 7월 20일
x = 5 ;
r = mod(x,2);
switch r
case 1
fprintf('%d is Odd\n',x)
case 0
fprintf('%d is Even\n',x)
end

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


David Hill
David Hill 2022년 7월 20일
Look at the function switch, case, otherwise, it does not work as you coded.
function [y] = even_odd(x)
r = mod(x,2);
if x<=4 && r==0
fprintf('even \n')
elseif x<=5 && r==1
fprintf('odd \n')
else
fprintf('i will get back to you on that \n')
end
end
  댓글 수: 1
cgo
cgo 2022년 7월 20일
sorry, the problem insists on using switch. I am practising the command since I know if-else already.

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

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by