登录论坛

查看完整版本 : [求助]switch语句中的otherwise


tguranus
2008-04-23, 17:54
程序段如下:
function distance=metric(x,y)
if x==y
distance=0;
else
distance=1;
end
switch(y)
case 0
if x==0
distance=0.0458;
end
if x==1
distance=2;
end
if x==2;
distance=1.0458;
end
case 1
if x==0
distance=2;
end
if x==1
distance=0.0458;
end
if x==2
distance=1.0458;
end
otherwise
break;
end
这是一个子程序,为了确保正确的返回量在switch语句的最后使用了otherwise break语句,但是程序执行时总是提醒我break出现在循环外,让我用return代替(A BREAK statement appeared outside of a loop. Use RETURN instead),可是我用return之后程序又陷入无限循环中,无法得到正确结果,请问这是什么原因造成的啊?

watcher
2008-04-24, 13:55
从这个程序看,return和无限循环没有关系,导致死循环的应该是调用metric的程序