# Thread: How to use if else statement?

1. ## How to use if else statement?

EXERCISE 3
Write a program to calculate a salary of a salesperson based on the unit of items sold.
 Get the staff name, salary and the units sold.
 Get the bonus percentage for sales as shown in the following table, based on the units sold.
o The bonus amount is the bonus percentage of the salary.
 Calculate the total salary for the salesperson.
 Display the name, salary, units sold, bonus percentage, bonus amount and total salary.
 Design your own format of an appropriate output for this program.

Hello guys. I need help to solve this kind of question. I have done my own programming but it seems to have some errors. I have tried and searched all the possible solutions to solve this matter but the outcomes are still the same. I would appreciate if someone can help me to solve this kind of problem. Thanks in advance.

Below is the code that I have done. I hope you guys can fix it or give me a clue/hint on how to solve this question.

Code:
```#include <stdio.h>
#include <string.h>

void main()
{
int unit;
float salary;
char staff[20] , bonus;

printf("\n-------------------------------------");
printf("\n               BONUS                 ");
printf("\n-------------------------------------");

printf("\nEnter the staff name : ");
gets (staff);
printf("\nEnter the salary : RM ");
scanf("%f", &salary);
printf("\nEnter the units sold : ");
scanf("%d", &unit);

salary = unit * bonus;

if (unit > 100 || unit < 200){
strcpy(bonus, "0.02");
}
else if (unit > 201 || unit < 300){
strcpy(bonus, "0.04");
}
else if (unit > 300){
strcpy(bonus, "0.06");
}

salary = unit * bonus;

printf("\nStaff name : %s ", staff);
printf("\nSalary : %f ", salary);
printf("\nUnits Sold : %d ", unit);
printf("\nBonus Percentage : %.2f ", bonus);
printf("\nBonus Amount : ");
printf("\nTotal Salary : RM");

return 0;
}```

2. Why is bonus a char? Seems to me a bonus should be a float, probably. You're trying to strcpy strings like "0.02" into bonus (by the way strcpy only works into strings, not single chars!)... wouldn't that be easier as [b]bonus = 0.02;[b]? This way the multiplication unit * bonus might actually work.

You seem to be printing bonus as a float, in fact: %.2f.

Your test conditions have some edge cases they don't handle. For example, if precisely 200 or 201 is entered, none of the bonuses will apply. Try <= and >=. Also it should probably be and's, not or's: every single number is either greater than 100 OR less than 200!

Remember you can avoid redundant boolean expressions by turning
Code:
```if(x >= 20 && x <= 30) ...
else if(x >= 10 && x <= 20) ...
else if(x >= 0 && x <= 10)```
into (say)
Code:
```if(x >= 20) ...
else if(x >= 10) ...
else if(x >= 0)```
or some such. (With an extra check for > 30 if you needed to.)

3. I've edited the codes but now I keep getting errors like "expected expression before '>' token".

I thought this is an expression
Code:
`if (unit => 100 && unit <= 200)`
By the way, can I know what does this "[b] means?
Code:
```[b]bonus = 0.02;[b]
```
This is the code I've edited so far.
Code:
```#include <stdio.h>

int main()
{
int unit;
float salary, bonus;
char staff;

printf("\n-------------------------------------");
printf("\n               BONUS                 ");
printf("\n-------------------------------------");

printf("\nEnter the staff name : ");
scanf("%c", &staff);
printf("\nEnter the salary : RM ");
scanf("%f", &salary);
printf("\nEnter the units sold : ");
scanf("%d", &unit);

salary = unit * bonus;

if (unit => 100 && unit <= 200) {
bonus =0.02;
}
else if (unit => 201 && unit <= 300){
bonus =0.04;
}
else (unit => 300){
bonus =0.06;
}

salary = unit * bonus;

printf("\nStaff name : %c ", staff);
printf("\nSalary : %f ", salary);
printf("\nUnits Sold : %d ", unit);
printf("\nBonus Percentage : %.2f ", bonus);
printf("\nBonus Amount : ");
printf("\nTotal Salary : RM ");

return 0;
}```

4. => should be >=

And else has no condition.

5. In the if...else if...else if... construction the idea is that the final "else" can be used to handle "exceptional" cases. For example
Code:
```    if (unit => 100 && unit <= 200) {
bonus =0.02;
}
else if (unit => 201 && unit <= 300){
bonus =0.04;
}
else if (unit => 300){
bonus =0.06;
}
else {
printf("Invalid unit entered. Program will exit.\n");
exit(127);
}```
That's just one possibility

6. And if unit is less than 100, what is bonus then?

What's the use of line 24 when you have line 38?