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


Groups > comp.programming > #654 > unrolled thread

question on a generic program logic issue

Started byNoName <null@null.com>
First post2011-07-16 02:37 -0400
Last post2011-07-18 10:09 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.programming


Contents

  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

#654 — question on a generic program logic issue

FromNoName <null@null.com>
Date2011-07-16 02:37 -0400
Subjectquestion on a generic program logic issue
Message-ID<9sb227hu62qg7afpfvghvi0iuqflboe7nn@4ax.com>
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.

I'm a hobbyist, not a pro, and a little rusty on my programming, and
just getting back into it.  So maybe I've forgotten some obvious way
to do this.  

I can come up with long, clunky approaches, but nothing elegant and
simple.  For example, I could use the flags to set up different
conditions in a switch statement, and then each case in the switch
statement represents a different combo of and/or flags;  but that gets
real long, especially if there's more than just three conditions.

If someone can show me some simple way to set this up, I'd be real
grateful.  

[toc] | [next] | [standalone]


#656

Fromkym@kymhorsell.com
Date2011-07-16 06:52 +0000
Message-ID<4e213520$0$22472$afc38c87@news.optusnet.com.au>
In reply to#654
In comp.programming NoName <null@null.com> wrote:
> 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?.
...

Maybe you can look for "decision tables" with google.

-- 
So you really, really believe that our universe just came about by
sheer chance?  I prersonally, find that extremely hard to accept.
-- BONZO@27-32-240-172 [100 nyms and counting], 11 Jan 2011 15:02 +1100

[toc] | [prev] | [next] | [standalone]


#657

FromWillem <willem@toad.stack.nl>
Date2011-07-16 09:05 +0000
Message-ID<slrnj22l2h.2d5n.willem@toad.stack.nl>
In reply to#654
NoName wrote:
) 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:

Is there only one flag that determines both and/or choices ?

Then I dont think you can do much better than:

 if ((and_flag && c1 && c2 && c3) || (!and_flag && (c1 || c2 || c3))) {
 }

Perhaps you can reduce that a bit with some boolean algebra,
but I doubt you can reduce it a lot.  Off the top of my head,
you can drop the first use of 'and_flag'.

If you have two separate flags, it becomes a lot more complicated.
Then you may be better off looking at decision tables.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [next] | [standalone]


#667

FromKeith Thompson <kst-u@mib.org>
Date2011-07-16 10:46 -0700
Message-ID<lnpqlanmoz.fsf@nuthaus.mib.org>
In reply to#654
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"

[toc] | [prev] | [next] | [standalone]


#674

FromWillem <willem@toad.stack.nl>
Date2011-07-18 10:09 +0000
Message-ID<slrnj281jc.sge.willem@toad.stack.nl>
In reply to#654
NoName wrote:
) 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?.

Another question:  What about the precedences ?

Normally, 
 if (condition1 or condition2 and condition3)

is equivalent to
 if (condition1 or (condition2 and condition3))

instead of
 if ((condition1 or condition2) and condition3)


Which would complicate matters quite a bit.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web