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:
return "None" if flag1, flag2 and flag3 are "N"
else return "Active" if flag1, flag2 and flag3 are "A"
else return "Inactive" if flag1, flag2 and flag3 are "I"
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.
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)
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.
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.
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.
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
}
[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);
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With