필터 지우기
필터 지우기

changine if-elseif to a switch-case structure

조회 수: 7 (최근 30일)
Mike
Mike 2013년 10월 30일
편집: John Kelly 2017년 11월 10일
rewrite the following code segment containing an if-elseif using a switch-case structure instead of the if-elseif. The code should not contain any ifs and should employ minimum number of disp commands. assume a is always a positive number
if a<60
disp('f)
elseif a<70
disp('d')
else
disp('good job')
my attempt was a=input(' ') switch a case a<60 disp('f') case a>= 60 & a<70 disp('d') otherwise disp('edit you') end
  댓글 수: 2
dpb
dpb 2013년 10월 30일
Homework needs an honest attempt first -- post your work and a specific question...meanwhile, there's always
doc switch
with and example to get you kick-started...
Mike
Mike 2013년 10월 30일
편집: John Kelly 2017년 11월 10일
a=input(' ') switch a case a<60 disp('f') case a>= 60 & a<70 disp('d') otherwise disp('edit you') end
my attempt.. please help got an exam soon

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

채택된 답변

sixwwwwww
sixwwwwww 2013년 10월 31일
Dear Mike, You can convert if-else code to switch case structure as follows:
InputValue = input('Input value: ');
val = sum(InputValue < [70 60]);
switch val
case 2
disp('f')
case 1
disp('d')
otherwise
disp('Good job')
end
I hope it helps. Good luck!
  댓글 수: 3
sixwwwwww
sixwwwwww 2013년 10월 31일
You are welcome
Sabri Çetin
Sabri Çetin 2016년 6월 22일
Can you generalize your answer for arbitrary conditions?
There is an obvious generalization by using several switch statements, but you did this with only one switch statement, but your answer somehow depends on the condition.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel Computing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by