More than 1 elseif statement with 3 different numbers

20 views (last 30 days)
I've been trying to figure this out without 2 separate if statements. Bunch of googling,etc... And still can't find an applicable answer
totalSales=input('Enter the total sales number of the employee ');
if totalSales < 5000 % less than 5000 total sales
additionalBonus = 0;
elseif totalSales < 10000 >= 5000 % less than 10000, but equal or greater than 5000 total sales
additionalBonus = totalSales * 0.03;
elseif totalSales >= 10000 % greater than or equal to 10000 total sales
additionalBonus = totalSales * 0.06;
end
I'm doing an example from a book, and I'm having trouble with the code recognizing the third statement.
If I write >= 10000 as a separate if statement, it works fine, but if I try if,elseif,else, it doesn't work.
6500 total sales for example, uses 0.06 instead of 0.03 function.
Do I have to use a separate if statement for the third function?
Here's the full code:
baseSalary=input('Enter the base salary of the employee ');
noOfServiceYears=input('Enter the number of service years for the employee ');
if (noOfServiceYears <= 5)
bonus = 10 * noOfServiceYears;
elseif (noOfServiceYears > 5)
bonus = 20 * noOfServiceYears;
end
totalSales=input('Enter the total sales number of the employee ');
if totalSales < 5000 % less than 5000 total sales
additionalBonus = 0;
elseif totalSales < 10000 >= 5000 % less than 10000, but equal or greater than 5000 total sales
additionalBonus = totalSales * 0.03;
elseif totalSales >= 10000 % greater than or equal to 10000 total sales
additionalBonus = totalSales * 0.06;
end
payCheck = baseSalary + bonus + additionalBonus;
  2 Comments
John D'Errico
John D'Errico on 11 Oct 2015
No. You did NOT figure it out. Read the answer by Stephen.
X < A < Y
does not do what you think it does. Yes, it is a common shorthand in mathematics, but not in MATLAB.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 11 Oct 2015
Edited: Stephen23 on 11 Oct 2015
You should have been a bit more patient answering and accepting your own question, because your "answer" is totally wrong. The code that you think works is actually doing something quite different to what you imagine. Lets have a look at what it really does...
The basic problem is that you are performing two logical relations simultaneously: MATLAB only supports one logical relational operation at once. In summary:
X<A<Y % is NOT valid for comparing A against X and Y
X<A & A<Y % This compares A with both X and Y
In MATLAB logical relational operators follow the standard rules of operator precedence. Look at this example carefully:
>> 3<4<2
ans =
1
The output is true, so does this mean that 4<2 ? Of course not, we just have to remember the operator precedence rules, just like in high school. This is evaluated according to those rules as:
>> (3<4)<2
ans =
1
where
>> 3<4
ans =
1
So 3<4<2 is actually equivalent to 1<2, which is of course very true:
>> 1<2
ans =
1
Now lets have a look at your answer. Whatever values you have for totalSales the answer is going to be true:
>> totalSales = 0;
>> totalSales >= 5000 < 10000
ans =
1
>> totalSales = 7500;
>> totalSales >= 5000 < 10000
ans =
1
>> totalSales = 20000;
>> totalSales >= 5000 < 10000
ans =
1
Can you see why? Because of operator precedence, it is actually equivalent to this:
(totalSales >= 5000) < 10000
Note that it does not matter if the first comparison totalSales >= 5000 produces a 0 or a 1 output, because both of 0 and 1 are less than 10000, so the answer is always going to be true.
The real solution for your original question was something like this:
totalSales = input('Enter the total sales number of the employee ');
if totalSales<5000
additionalBonus = 0;
elseif totalSales<10000
additionalBonus = totalSales * (0.03);
else
additionalBonus = totalSales * (0.06);
end
The comparisons totalSales >= 5000 and totalSales >= 10000 are totally superfluous, so I removed them. Obviously the value must be greater than 5000, because if it wasn't it would be caught by the first if comparison, ditto the greater than 10000.
  5 Comments
Rik
Rik on 29 Nov 2021
@Julian Perge How should this answer have been phrased? If your answer was incorrect, how should that have been worded? And if it wasn't, can you explain why this answer is incorrect? If you don't like the tone, can you explain what exactly should be rephrased in order for you to be able to accept this answer? You can also write up your own answer. Don't immediately accept it, so we have the opportunity to suggest any edits that might make it easier for novices to understand.
This is an important question, which is often asked, so it merits a thorough (and correct) answer.
I'm sorry you feel these responses were hostile. I'm confident this wasn't the intention of either Stephen or John. Remember that it is difficult to convey tone of voice in text, especially if not all participants are reading/writing in their native language.
I will be restoring the original question, as this thread clearly serves a purpose, as Adam also indicates.
Jan
Jan on 2 Dec 2021
@Julian Perge: In this forum we concentrate on solving Matlab problems. The contents of the forum is thought as database for Matlab users searching for help and solutions. Therefore editors and admins care about the correctness of answers.
Of course there can be cases, in which different opinions co-exists and there is no unique solution. Sometimes users do not agree and especially in such cases it is the goal of this forum to keep calm and polite. This helps to solve Matlab problems and to avoid flamewars and frustration.
I do not see aggressiv formulations or insults in the contributions of John and Stephen. Remember, that they are some foreign people from the internet, who spend their time an effort voluntarily to help others. They might have a different cultural background, so do not interprete to much emotions in their formulations. I recommend to select the parts of the answers, which solve the problem, to ignore the rest and not to take the statements personally.
You see, that members of this forum care about your flags. Let me mention the Community Guidelines:
Do not:
  • 6. Chastise newcomers or insult other members
  • 14. Edit posts to make them unreadable
  • Respond to questions/comments if you feel your effort isn't well spent
Thank you for repsecting this.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!