Store if-else statement

if (status == 0)
{
double a = 6000, b = 27950;
if (income <= a)
tax = income * 0.10;
else if (income <= b)
tax = a * 0.10 + (income - a) * 0.15;
else
tax = a * 0.10 + (b - a) * 0.15 + (income - b) * 0.27;

}

else if (status == 1)
{
double a = 12000, b = 34000;
if (income <= a)
tax = income * 0.10;
else if (income <= b)
tax = a * 0.10 + (income - a) * 0.15;
else
tax = a * 0.10 + (b - a) * 0.15 + (income - b) * 0.27;

}

The nested if-else statements have no difference except their input values from variable a, b. I am wondering if I can store the repeating if-else statement into something and put it under those status...

Example:
if (status == 0)
{
double a = 6000, b = 27950;
//Trigger the stored if-else statement, input the variable.
}

else if (status == 1)
{
double a = 12000, b = 34000;
//Trigger the stored if-else statement, input the variable.

Is it possible?
Last edited on
You have (at least) two different ways you can choose to structure your program.

1: Create a function to factor out common behavior

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
double calculate_tax(double income, double a, double b)
{
    double tax;
    if (income <= a)
        tax = income * 0.10;
    else if (income <= b)
        tax = a * 0.10 + (income - a) * 0.15;
    else
        tax = a * 0.10 + (b - a) * 0.15 + (income - b) * 0.27;
    return tax;
}

...

if (status == 0)
{
    double a = 6000, b = 27950;
    tax = calculate_tax(income, a, b); //Trigger the stored if-else statement, input the variable.
}

else if (status == 1)
{
    double a = 12000, b = 34000;
    tax = calculate_tax(income, a, b);
}



2: Factor out the variable assignment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double a = 0; // default values of 0, in case neither status 1 or 2
double b = 0;

if (status == 0)
{
    a = 6000;
    b = 27960;
}
else if (status == 1)
{
    a = 12000;
    b = 34000;
}

if (income <= a)
    tax = income * 0.10;
else if (income <= b)
   tax = a * 0.10 + (income - a) * 0.15;
else
   tax = a * 0.10 + (b - a) * 0.15 + (income - b) * 0.27;


You could combine them, as well.

Last edited on
That's work thank you!
two concepts that might help you:
a "state" variable, like an enum, can store a meta. Taken as a binary it is just a surrogate boolean {true, false} but extended you can get {situation1, situation2, situation3, ...}

consider (you can apply namespaces and cleanup, this is just the simple example)

enum incomelevel{LTa, LTb, GTboth, max_il}; //less than a, less than b, or greater than both

incomelevel stored_if(double income, double a, double b)
{
if(income < a)
return LTa;
if(income <b)
return LTb;
return GTboth;
}

you may also consider a switch statement, either stand-alone or using the above concept to trip off the enum from the stored if. For examplel
incomelevel result = stored_if(income, a, b);

switch(result)
case: LTa
... etc

you can do the same thing with bool for binary checks (bool checksomething(...) function).

Last edited on
Topic archived. No new replies allowed.