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


Groups > comp.programming > #667

Re: question on a generic program logic issue

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
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 Sat, 16 Jul 2011 10:46:04 -0700
Organization None to speak of
Lines 61
Message-ID <lnpqlanmoz.fsf@nuthaus.mib.org> (permalink)
References <9sb227hu62qg7afpfvghvi0iuqflboe7nn@4ax.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Injection-Info mx04.eternal-september.org; posting-host="mytEQcPL+ceHcrnNa7VoaQ"; logging-data="25542"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+q9spLtoh+Mf5KMwqgkjGm"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:HvYhqHlDuMYuhhi4hJCk3fcDdM0= sha1:oaBekmqytWugulXkABbwdwXp17M=
Xref x330-a1.tempe.blueboxinc.net comp.programming:667

Cross-posted to 2 groups.

Show key headers only | 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