Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex if else logic

Tags:

c#

logic

Friends

How do I implement following complex logic?

flag1 can be "N" or "A" or "I"
flag2 can be "N" or "A" or "I"
flag3 can be "N" or "A" or "I"

function (string flag1, string flag2, string flag3) begin

The function needs to return:

  1. return "None" if flag1, flag2 and flag3 are "N"

  2. else return "Active" if flag1, flag2 and flag3 are "A"

  3. else return "Inactive" if flag1, flag2 and flag3 are "I"

  4. else return "both" if flag1, flag2 and flag3 are either "A" AND "I" (OR "N")

e.g. 1) flag1 is "A" and flag2 is "I" and flag3 is "I"
e.g. 2) flag1 is "I" and flag2 is "A" and flag3 is "I"
e.g. 2) flag1 is "A" and flag2 is "N" and flag3 is "I"

retrun result

end

Thanks for reply but none of post gives answer. I know if else constrauct and looking for logic to implement above psedocode. All four are possibel conditions specially #4 is complex and need to know how to implement that.

like image 763
Kumar Avatar asked Aug 02 '10 19:08

Kumar


People also ask

How do you put 2 conditions in if Excel?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

How do you do the if-else condition?

Conditional Statements Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

What is if-else condition structure?

The if-else is statement is an extended version of If. The general form of if-else is as follows: if (test-expression) { True block of statements } Else { False block of statements } Statements; n this type of a construct, if the value of test-expression is true, then the true block of statements will be executed.

What is nested if-else?

A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.


3 Answers

Your logic for point 4 is confusing...

I would use an enum value for this, rather than strings - it is much more type-safe (eg what if someone passed "WIBBLEWOBBLE" to your method? What should it return?)

enum Value { None, Active, Inactive, Both }

private Value GetValue(Value flag1, Value flag2, Value flag3) {
    if (flag1 == flag2 && flag2 == flag3)    // they are all the same
        return flag1;
    else return Value.Both;    // there is a difference
}
like image 198
thecoop Avatar answered Sep 28 '22 02:09

thecoop


[Flags]
enum SomeWierdReturn
{ Both = 0, None = 1, Active = 2, Inactive = 4 }

public SomeWierdReturn DoSomething(SomeWierdReturn flag1, SomeWierdReturn flag2, SomeWierdReturn flag3)
{
    return (SomeWierdReturn)(flag1 & flag2 & flag3);
}
like image 26
Jimmy Hoffa Avatar answered Sep 28 '22 01:09

Jimmy Hoffa


I think it serves both readability and speed if you first check if all 3 values are equal.

if ((flag1 == flag2) and (flag1 == flag3))
   // use a switch or table to go 'I' -> Inactive etc
else
  return "Both"; // as far as i understood 4)
like image 43
Henk Holterman Avatar answered Sep 28 '22 01:09

Henk Holterman