|
Vittorio Beginner
Joined: 22 Oct 2003 Posts: 10
|
Posted: Mon Nov 17, 2003 5:13 am
OR evaluation |
I'm sure someone must have asked this question before, but I can't figure out what to search for.
When you have a condition with multiple ORs.. like #IF (cond1|cond2|cond3), does it stop evaluating as soon as one of them is true? ie if cond1 is true.. will cond2 and cond3 be evaluated? |
|
|
|
Cuttlefish Apprentice
Joined: 28 Oct 2003 Posts: 164
|
Posted: Mon Nov 17, 2003 5:26 am |
Yeah, try this:
Code: |
#if ((%prompt( 0, "Prompt1")=0)|(%prompt( 0, "Prompt2")=0)) {#show "match"} {#show "nomatch"} |
It shows both prompts even if you enter 0. So no short-circuiting for you. |
|
|
|
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Mon Nov 17, 2003 5:59 am |
You don't know comparative logic. Here's a brief lesson:
OR
1)an OR statement can only have two parts, A and B, resulting in this statement:
A OR B
2)If A is true, B is true, or both A and B are true, the entire OR statement is true; if both A and B are false, the entire OR statement is false
Since cond1|cond2|cond3 has a total of three parts, we need to break down the statement as follows:
cond1|cond2
answer to (cond1 OR cond2)|cond3
The moment a TRUE is discovered, processing stops.
AND
1)an AND statement can only have two parts, A and B, resulting in the following expression:
A AND B
2)if A is true and B is true, then A AND B is true; if either A or B (or both) are false, then A AND B is false
Given an expression like Cond1 AND cond2 AND cond3, you end up with the following comparisons:
Cond1 AND Cond2
answer to (Cond1 AND Cond2) AND Cond3
As soon as a FALSE is discovered, processing stops. |
|
|
|
Cuttlefish Apprentice
Joined: 28 Oct 2003 Posts: 164
|
Posted: Mon Nov 17, 2003 6:08 am |
Um, did you actually try it? Like maybe the example I gave above.
|
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Mon Nov 17, 2003 7:31 am |
Very old programming lesson, still true of just about every language, follow the order of operations to determine what is done first. Order of ops clearly states that () is first so Cuttlefishes example will definitely do both prompt tests before even looking at the OR. Even without the parenthesis, equals is above OR so again both prompts would have to be tested.
The same programming lesson is one on optimization.
#IF (a&b&c) {d} can be rewritten to #IF (a) {#IF (b) {#IF (c) {d}}}
This allows the whole test to fail sooner, prioritizing the least like condition into a helps even more.
#IF (a|b|c) {d} can be rewritten to #IF (!a) {#IF (!b) {#IF (c) {d}} {d}} {d}
Here I reversed the logic slightly to put all the code next to each other. Normally you would want to use an alias for a larger code chunk there for ease of modification. |
|
|
|
Cuttlefish Apprentice
Joined: 28 Oct 2003 Posts: 164
|
Posted: Mon Nov 17, 2003 8:04 am |
(Edit: when I say most languages, I mean most languages with short-circuiting)
Erm, am I missing something? In most languages I've dealt with, parens are only for grouping. For example, pascal:
if funca or (funcb) then
writeln('done');
or
if (funca) or (funcb) then
writeln('done');
If funca returns true, funcb never executes.
In C:
if (funca()||(funcb())) printf("donen");
or
if ((funca())||(funcb())) printf("donen");
Again, funcb never gets executed. Try it out yourself.
Going back to the example, you can change it to:
#if (%prompt( 0, "Prompt1")|%prompt( 0, "Prompt2")) {#show "match"} {#show "nomatch"}
Even if you type 1 in the first prompt box, it shows the second one.
Just to give you a second example:
#if (1|%prompt( 0, "Prompt2")) {#show "match"} {#show "nomatch"}
Pops up the prompt.
The basic point, as I tried to show in my first post, was that zMUD doesn't do short-circuit evaluation. Now, whether zMUD uses parens to force evaluations (which most languages using short-circuiting DO NOT do), I can't tell you.
However, this whole discussion has brought up some other questions I'm going to bring up in another thread. |
|
|
|
Vittorio Beginner
Joined: 22 Oct 2003 Posts: 10
|
Posted: Mon Nov 17, 2003 8:23 am |
Thanks. I asked because I was wondering if I should use nested IF statements to optimize the evaluation somewhat. Looks like I'll have to..
|
|
|
|
Cuttlefish Apprentice
Joined: 28 Oct 2003 Posts: 164
|
Posted: Mon Nov 17, 2003 8:43 am |
Also, note that:
#if (%prompt( 0, "Prompt1") & %prompt( 0, "Prompt2")) {#show "match"} {#show "nomatch"}
Gives you both prompts when you enter a 0 in prompt1, even though there's NO WAY that prompt2 could be make the whole statement true.
So, though Vijilante writes:
quote: The same programming lesson is one on optimization.
#IF (a&b&c) {d} can be rewritten to #IF (a) {#IF (b) {#IF (c) {d}}}
zMUD evaluates "#IF (a&b&c) {d}" as:
Code: |
result=a
result=result & b
result=result & c
#if (result) {d}
|
It's all about the short-circuiting. |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|