why i cannot genereate the answer?

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 Comments

Hi Melissa,
Below a solution picture (remember to name function correctly):
It is very important the name that you use to save your function.
Below the code:
function y= postiveneg(a)
if a>0
disp('positve');
else; if a<0
disp('negative'), y=a;
else
disp('zero');
end
end
end
Stephen23
Stephen23 on 6 Sep 2018
Edited: Stephen23 on 6 Sep 2018
@Cesar Antonio Lopez Segura: why do you add an extra if - else statement, instead of just fixing the else if syntax mistake? Adding another if - else just makes the code more complex than it needs to be, and more complex code is buggier code.
Please focus on Melissa error question and Melissa code.
Guillaume
Guillaume on 6 Sep 2018
Edited: Guillaume on 6 Sep 2018
Stephen is focused on the error and code. As the error message states a end is missing in her code. That end would not be missing if melissa had written elseif instead of else if. Changing else if to elseif is all that is needed to fix the bug.
Stephen is correct that semantically elseif and else if are different. It is clear from the code that elseif was meant.
I am not agree with you Guillaume . In fact following your recommendation Melissa will need to fix her code:
*
I think that this extra simicolon is unnecessary for this reason the program detect a warning in line number 2.
Guillaume
Guillaume on 6 Sep 2018
Edited: Guillaume on 6 Sep 2018
Yes, it's better to get rid of all mlint warnings. However, that semicolon does not affect the behaviour of the code in any way. Again, all that was needed to get rid of the error is to remove the extra space between else and if.
Note: it's interesting that mlint shows the unnecessary colon at the end of the function line but fails to pick up the equally unnecessary colon that you wrote in your else; if a<0 line.
Does it have any effect after disp()?
Stephen23
Stephen23 on 6 Sep 2018
Edited: Stephen23 on 6 Sep 2018
"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?
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

Answers (3)

Stephen23
Stephen23 on 6 Sep 2018
Edited: Stephen23 on 6 Sep 2018
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 Comments

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).
"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
KSSV
KSSV on 6 Sep 2018
In the file: for the function postiveneg enter/type end at the end of the line.
Dennis
Dennis on 6 Sep 2018
Edited: Dennis on 6 Sep 2018
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

This question is closed.

Tags

Asked:

on 6 Sep 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!