And connectors

For builders to discuss and ask building questions.
Post Reply
Mask
Staff
Staff
Posts: 2649
Joined: Tue Jul 15, 2003 9:21 pm

And connectors

Post by Mask » Sun Jul 08, 2007 1:22 am

One other useful thing I just added was 'and' connectors. For example:

Code: Select all

>speech_prog howdy~
if quest ( $n, 0, 4 ) >= 0
and quest ( $n, 0, 4 ) < 4 then
  say Increasing quest bits from zone one
  mpmadd $n quest 0 4 1
else
  if quest ( $n, 0, 4 ) >= 4
  and quest ( $n, 0, 4 ) < 8 then
    say Increasing quest bits from zone two
    mpmadd $n quest 0 4 1
  else
    if quest ( $n, 0, 4 ) >= 8
    and quest ( $n, 0, 4 ) < 12 then
      say Increasing quest bits from zone three
      mpmadd $n quest 0 4 1
    endif
  endif
endif
~
This should make some blocks of logic somewhat simpler...
User avatar
Lathlain
Sword Grand Master
Sword Grand Master
Posts: 1169
Joined: Sun Aug 10, 2003 4:25 pm
Location: Zhentil Keep

Post by Lathlain » Sun Jul 08, 2007 8:58 am

You little beauty! You've been working hard! Cheers, these will go to good use.
Dalvyn
Sword Grand Master
Sword Grand Master
Posts: 4708
Joined: Tue Jul 15, 2003 9:26 pm
Location: House of Wonder, Waterdeep

Post by Dalvyn » Sun Jul 08, 2007 9:58 am

Those will help a lot indeed.

Just to nitpick ... a more efficient version of your first program (with less redundancy) actually does not use "and" connectors:

Code: Select all

>speech_prog howdy~
if quest (0,4,$n) >= 0
  if quest(0,4,$n) < 4
    say Increasing quest bits from zone one
    mpmadd $n quest 0 4 1
  else
    if quest(0,4,$n) < 8
      say Increasing quest bits from zone two
      mpmadd $n quest 0 4 1
    else
      if quest(0,4,$n) < 12
        say Increasing quest bits from zone three
        mpmadd $n quest 0 4 1
      endif
    endif
  endif
endif
~
Where the addition of "and" connectors really shines though is in situations where a program is supposed to perform something if two conditions are true and something else otherwise.

Without the "and", we had to duplicate the "do something_else" code :

Code: Select all

if A
  if B
    do something
  else
    do something_else
  endif
else
  do something_else
endif
And it even got worse if there were more than 2 conditions:

Code: Select all

if A
  if B
    if C
      if D
        do something
      else
        do something_else
      endif
    else
      do something_else
    endif
  else
    do something_else
  endif
else
  do something_else
endif
Thanks to the "and", we can now write

Code: Select all

if A
and B
  do something
else
  do something_else
endif
and

Code: Select all

if A
and B
and C
and D
  do something
else
  do something_else
endif
Thanks for this addition.
Image
Dalvyn
Sword Grand Master
Sword Grand Master
Posts: 4708
Joined: Tue Jul 15, 2003 9:26 pm
Location: House of Wonder, Waterdeep

Post by Dalvyn » Sun Jul 29, 2007 9:55 pm

A question about the addition of AND.

When we only had OR, complex conditions had only one possible meaning. For example,

Code: Select all

if A
or B
or C
clearly meant "if (at least) one condition amongst A, B and C is satisfied, ...".

This is not so straight-forward with a code like

Code: Select all

if A
or B
and C
which could be interpreted as "if (A or B) and C" or as "if A or (B and C)".

Those two things have completely different meanings.

For example,

if A is "$n is a male",
if B is "$n is a female", and
if C is "$n has a child",

then

(A or B) and C means "$n is male or $n is female" AND "$n has a son", which basically amount to "$n has a child" (neuter sex notwithstanding); while

A or (B and C) means "$n is male" or "$n is female and $n has a child", that is "$n is male" or "$n is a mother".
Image
Arnof
Sword Journeyman
Sword Journeyman
Posts: 129
Joined: Wed Oct 22, 2008 8:47 pm
Location: Arizona, USA
Contact:

Re:

Post by Arnof » Tue Jan 05, 2010 6:26 pm

I was wondering which of Dalvyn's scenarios ends up being true? I'll write the lesson on this and send it to Mask when I'm certain so it can be added to the builders' lessons.

I'm still interested in establishing the wiki, even though I haven't made any move on it since... Since earlier this year when we first discussed it.

B
Dalvyn wrote:A question about the addition of AND.

When we only had OR, complex conditions had only one possible meaning. For example,

Code: Select all

if A
or B
or C
clearly meant "if (at least) one condition amongst A, B and C is satisfied, ...".

This is not so straight-forward with a code like

Code: Select all

if A
or B
and C
which could be interpreted as "if (A or B) and C" or as "if A or (B and C)".

Those two things have completely different meanings.

For example,

if A is "$n is a male",
if B is "$n is a female", and
if C is "$n has a child",

then

(A or B) and C means "$n is male or $n is female" AND "$n has a son", which basically amount to "$n has a child" (neuter sex notwithstanding); while

A or (B and C) means "$n is male" or "$n is female and $n has a child", that is "$n is male" or "$n is a mother".
Post Reply