Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #667

Re: question on a generic program logic issue

From Keith Thompson <kst-u@mib.org>
Newsgroups alt.comp.lang.learn.c-c++, comp.programming
Subject Re: question on a generic program logic issue
Date 2011-07-16 10:46 -0700
Organization None to speak of
Message-ID <lnpqlanmoz.fsf@nuthaus.mib.org> (permalink)
References <9sb227hu62qg7afpfvghvi0iuqflboe7nn@4ax.com>

Cross-posted to 2 groups.

Show all headers | View raw


NoName <null@null.com> writes:
> I'm trying to figure out how to do something in code, fairly
> efficiently, using fairly standard programming constructs.  The exact
> language doesn't matter, could be C, C++, Java, Visual Basic, C#, etc.
>
> The idea is this.  I want to have some code that basically says:
>
> if (condition1 and/or condition2 and/or condition3) then….
>
> where the "and/or" is determined by some kind of flag that can be set
> dynamically.  Viewed another way, in pseudocode, I want:
>
> if 
> (c1
> use "and" if and_flag=true else use "or" if or_flag=true
> c2
> use "and" if and_flag=true else use "or" if or_flag=true
> c3) then...
>
> I'm trying to figure out how to code this efficienty using standard
> kinds of procedural constructs:  if/then, if/then/else, case or switch
> statements, etc.

That's an odd requirement, but (warning, untested code follows):

bool and_or(bool x, bool y) {
    if (and_flag) {
        return x && y;
    }
    else {
        return x || y;
    }
}

if (and_or(condition1, and_or(condition2, condition3)) {
    ...
}

If you inline the function (assuming your language anc compiler support
inlining), it should be reasonably efficient.

A 3-argument version might be convenient:

bool and_or3(bool x, bool y, bool z) {
    if (and_flag) {
        return x && y && z;
    }
    else {
        return x || y || z;
    }
}

if (and_or3(condition1, condition2, condition3)) {
    ...
}

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

question on a generic program logic issue NoName <null@null.com> - 2011-07-16 02:37 -0400
  Re: question on a generic program logic issue kym@kymhorsell.com - 2011-07-16 06:52 +0000
  Re: question on a generic program logic issue Willem <willem@toad.stack.nl> - 2011-07-16 09:05 +0000
  Re: question on a generic program logic issue Keith Thompson <kst-u@mib.org> - 2011-07-16 10:46 -0700
  Re: question on a generic program logic issue Willem <willem@toad.stack.nl> - 2011-07-18 10:09 +0000

csiph-web