# Thread: if else switch ????? which one ?

1. ## if else switch ????? which one ?

why wont this work ?

i tried it first with if...else
Code:
```/*why will this program not run? what did i do wrong with the if else statements/conditions ?*/

#include <conio.h>
#include <iostream.h>
main()
{
double sales, sales_amount, quantity, total_sales, vat;
total_sales = sales + vat;
vat = 0.175*sales;

cout << "\nTotal Sales = sales amount * quantity\n";  endl;
cout << "\nEnter the sales amount\n";
cin >>  sales_amount;
cout << "\nEnter the quantity\n" ;
cin >>  quantity ;
sales = (sales_amount * quantity);
cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
cout << "\n" << "Sales is: \t" << sales << "\n";
cout << "\ntotal sales inc VAT is: \t" << (sales + 0.175*sales) << "\n";

if (sales < 25) cout << "\nNo commission";                                         //<-------------why will this not run?
else if (50 > sales < 25) cout << "\n 5% commission   " << total_sales * 0.05;
else if (75>sales<51) cout << "\n 10% commission    "; << total_sales * 0.10;
else if (100>sales<76)cout << "\n 20% commission"; << total_sales * 0.20;
else cout << "20% commission" ;
getch();

}```
i tried it with a switch statement

Code:
```#include <conio.h>
#include <iostream.h>
main()
{
double sales, sales_amount, quantity, total_sales, vat;
total_sales = sales + vat;
vat = 0.175*sales;

cout << "\nTotal Sales = sales amount * quantity\n";  endl;
cout << "\nEnter the sales amount\n";
cin >>  sales_amount;
cout << "\nEnter the quantity\n" ;
cin >>  quantity ;
sales = (sales_amount * quantity);
cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
cout << "\n" << "Sales is: \t" << sales << "\n";
cout << "\ntotal sales inc VAT is: \t" << (sales + 0.175*sales) << "\n";

switch(sales) {
case <25:
cout << "\nNo commission";
break;
case 50 > sales < 25:
cout << "\n 5% commission   " << total_sales * 0.05;
break;
case 75 > sales < 51:
cout << "\n 10% commission    "; << total_sales * 0.10;
case 100 > sales < 76:
cout << "\n 20% commission"; << total_sales * 0.20;
default:
cout << "\n 20% commission";
}
getch();
}```

2. You can't use this as a condition in an if statement.
Code:
`if(50 > sales < 25)`
However, you can use
Code:
`if(sales > 25 && sales < 50)`
where "&&" means "and". ("||" means "or".)

As for switch statements, the case labels have to be integers, so that's not very helpful.

3. Also, this is wrong
Code:
``` 	if (sales < 25) cout << "\nNo commission";                                         //<-------------why will this not run?
else if (50 > sales < 25) cout << "\n 5% commission   " << total_sales * 0.05;
else if (75>sales<51) cout << "\n 10% commission    "; << total_sales * 0.10;
else if (100>sales<76)cout << "\n 20% commission"; << total_sales * 0.20;
else cout << "20% commission" ;```

when you use an if statement, or a for loop, if you do not use brackets, then the only line of code that will be ran is the SINGLE LINE IMMEDIATELY FOLLOWING IT
for example

Code:
```if(blar >1 )
Runsomething();
Runsomethingelse();```
Runsomethinglese() should not be thought of as part of the if statement. You must use braces. Same rule for for loops, while loops

4. I don't think he meant to put those semicolons in the chained output expressions.

5. Try and read expression such as this
Code:
`50 > sales < 25`
aloud to yourself.

You may find yourself wanting to use the word "between", but there is no such operator as "between". You have to read < as "less-than" and > as "greater-than" etc.

You'll probably also find yourself wanting to use the word "and" in your description, but this does not match what you wrote because there was no && (read as "and") in that expression.
This actually indicates what the mistake is. You do have two conditions there, so using an && is actually necessary.
"sales is greater-than 25 AND sales is less-than 50".
Why do we have to mention sales twice? Because you have to be able to break up ANDed expressions into individual expressions and "is less than 50" is not valid as nobody would know what you are talking about.

You cannot use double in a switch.
Lastly, what happens if sales comes out to exactly 25 or exactly 50?!

6. Originally Posted by iMalc
Try and read expression such as this
Code:
`50 > sales < 25`
aloud to yourself.

You may find yourself wanting to use the word "between", but there is no such operator as "between". You have to read < as "less-than" and > as "greater-than" etc.

You'll probably also find yourself wanting to use the word "and" in your description, but this does not match what you wrote because there was no && (read as "and") in that expression.
This actually indicates what the mistake is. You do have two conditions there, so using an && is actually necessary.
"sales is greater-than 25 AND sales is less-than 50".
Why do we have to mention sales twice? Because you have to be able to break up ANDed expressions into individual expressions and "is less than 50" is not valid as nobody would know what you are talking about.

You cannot use double in a switch.
Lastly, what happens if sales comes out to exactly 25 or exactly 50?!

i tried tjis and it works better. Thanks

Code:
```#include <conio.h>
#include <iostream>

int main()
{
double sales, sales_amount, quantity, total_sales, vat;
total_sales = sales + vat;
vat = 0.175*sales;

cout << "\nTotal Sales = sales amount * quantity\n" << endl;
cout << "\nEnter the sales amount\n";
cin >>  sales_amount;
cout << "\nEnter the quantity\n" ;
cin >>  quantity ;
sales = (sales_amount * quantity);
cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
cout << "\n" << "Sales is: \t" << sales << "\n";
total_sales = (sales + 0.175 * sales);
cout << "\ntotal sales inc VAT is: \t" << total_sales << "\n";

if(total_sales<25)
cout << "\nNo commission";                                         //<-------------why will this not run?
else if(total_sales<50)
cout << "\n5% commission of total sales is " << 0.05 * total_sales;
else if(total_sales<75)
cout << "\n10% commission of total sales is " << 0.10 * total_sales;
else if(total_sales>75)
cout << "\n20% commission of total sales is " << 0.20 * total_sales;
getch();

}```