Info
此问题已关闭。 请重新打开它进行编辑或回答。
why i cannot genereate the answer?
1 次查看(过去 30 天)
显示 更早的评论
function y= postiveneg(a);
if a>0
disp('positve');
else if a<0
disp('negative'), y=a;
else
disp('zero');
end
A=postiveneg(100)
Error: File: postiveneg.m Line: 2 Column: 2
At least one END is missing: the statement may begin her
9 个评论
Stephen23
2018-9-6
编辑:Stephen23
2018-9-6
"I think that this extra simicolon is unnecessary for this reason the program detect a warning in line number 2."
Sure, that semicolon is not required and can be removed (as can most of the others). However note that that semicolon causes absolutely no error (just a warning), and is totally unrelated to the cause of the error that the original question is about, so it is unclear what your point is, or how this is any way relevant to your statement "I am not agree with you Stephen".
@Dennis: no, they make no difference. None of the semicolons are required, apart from the one after y=a;.
@Guillaume: my version of MATLAB (R2012b, default mlint settings) picks it up:
Perhaps this can be set in the mlint options?
Cesar Antonio Lopez Segura
2018-9-6
编辑:Cesar Antonio Lopez Segura
2018-9-7
Hi all, I have the same response as you in Matlab R2012b (default) but it is not the same in my R2016b version and I do not rememeber to change the default mlint options of my program.
In any case, I am in complete agreement with fix the mlint warnings which is in my opinion completely independent of function's errors.
Best regards and thank you for extremely enriching and interesting debate
Cesar
回答(3 个)
Stephen23
2018-9-6
编辑:Stephen23
2018-9-6
This:
else if a<0
is not really valid MATLAB syntax. The correct syntax is
elseif a<0
^^ no space!
written as one word. You would also have learned this by reading the if documentation, which is why it is recommended to read the documentation for every operation that you use, not matter how trivial it might seem. You should also pay attention to the other warnings that the editor shows.
2 个评论
Cesar Antonio Lopez Segura
2018-9-6
It is not necessary to create a function to detect positive negative values (obviously). And elseif is a recommendation no mandatory, in fact the code run perfectly without errors (that might sow confusion).
Stephen23
2018-9-6
编辑:Stephen23
2018-9-6
"And elseif is a recommendation no mandatory..:"
No, ELSEIF is not a "recommendation", it is actually the exact syntax that MATLAB uses for any extra statements that require a condition, exactly as described in the IF documentation. Please show a reference to the MATLAB documentation where ELSEIF is described as being a "recommendation".
What you did is you added a totally new IF-ELSE-END statement, and now you seem to claim that as being a good idea (it isn't: more complex code leads to more bugs, is harder to understand, is harder to maintain, ...)
Following your advice exactly, you are seriously suggesting that this is good code:
if A
...
else
if B
...
else
if C
...
else
if D
...
else
...
end
end
end
end
Compared to actually reading the MATLAB documentation and writing this much simpler code:
if A
...
elseif B
...
elseif C
...
elseif D
...
else
...
end
"..in fact the code run perfectly without errors"
Please provide an actual reason why your suggestion is a good idea (I already gave three reasons why it isn't). Anyone can also add lots of pointless operations to code, would you say that this is a good idea, just because "the code runs without errors"? For example, I guess you would prefer this, because it "runs without errors", and that is a good enough reason for you to use it:
>> -i*log(-1000/(1e3))
ans = 3.1416
Yet the MATLAB documentation gives this, the best, simplest, least buggy way to get that value:
>> pi
ans = 3.1416
Dennis
2018-9-6
编辑:Dennis
2018-9-6
I agree with Stephen and Guillaume that else if should be elseif instead.
I'd like to point out that there is another problem with your function: you assign y only for negative values, which will cause an error when calling your function with non-negative values. There are several ways to fix this, one would be to assign an empty value to y.
function y= postiveneg(a)
y=[];
if a>0
disp('positve')
elseif a<0
disp('negative')
y=a;
else
disp('zero')
end
end
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!