f

#### A && B && ( C && D )

```Hi all,

I've four conditions expression, A, B, C, and D.

Are the following two forms equivalent or not?

A && B && ( C && D )
A && B && C && D

Regards
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
```
 0
Hongyi
11/24/2015 4:41:28 AM
comp.unix.shell 15484 articles. 3 followers.

8 Replies
2304 Views

Similar Articles

[PageSpeed] 59

```Hongyi Zhao <hongyi.zhao@gmail.com> wrote:

>Are the following two forms equivalent or not?
>
>A && B && ( C && D )
>A && B && C && D

Yes. In either case, _all_ variables must be true for the expression to
be true.

The AND operator is "associative". In this wiki page, find the
subheading "Propositional logic", where "OR" is represented by a symbol
that looks like "V", and "AND" by an inverted "V".

https://en.wikipedia.org/wiki/Associative_property
```
 0
Dave
11/24/2015 8:57:01 AM
```Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> wrote:

>The AND operator is "associative". In this wiki page, find the
>subheading "Propositional logic", where "OR" is represented by a symbol
>that looks like "V", and "AND" by an inverted "V".
>
>https://en.wikipedia.org/wiki/Associative_property

That wiki page is for associativity generally, but I see there's also a
page specifically about programming language operator associativity:

https://en.wikipedia.org/wiki/Operator_associativity
```
 0
Dave
11/24/2015 9:04:05 AM
```Hongyi Zhao wrote:

> I've four conditions expression, A, B, C, and D.
>
> Are the following two forms equivalent or not?
^^^^^^^^^^^^^^^^^
> A && B && ( C && D )
> A && B && C && D

Yes.

--
PointedEars

Please do not cc me. / Bitte keine Kopien per E-Mail.
```
 0
Thomas
11/24/2015 1:30:30 PM
```On 24.11.2015 05:41, Hongyi Zhao wrote:
> Hi all,
>
> I've four conditions expression, A, B, C, and D.
>
> Are the following two forms equivalent or not?
>
> A && B && ( C && D )
> A && B && C && D

No, you can't rely on equivalence of those commands, because
in the first sample C and D are executed in a subshell.

See the following testcase...

Assume the (bash-)programs A B C D contain

echo \$0 \$BASH_SUBSHELL

then with bash you will get...

\$ bash -c 'A && B && C && D'
./A 0
./B 0
./C 0
./D 0

\$ bash -c 'A && B && (C && D)'
./A 0
./B 0
./C 1
./D 1

Now assume a conditional in the programs that is depending
on BASH_SUBSHELL, then your program sequences will behave
differently. You can easily check that if you define in your
programs just

exit \$BASH_SUBSHELL

then D will not be executed since C returns with an error
condition.

Janis

>
> Regards
>

```
 0
Janis
11/24/2015 2:27:27 PM
```Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

>On 24.11.2015 05:41, Hongyi Zhao wrote:
>> Hi all,
>>
>> I've four conditions expression, A, B, C, and D.
>>
>> Are the following two forms equivalent or not?
>>
>> A && B && ( C && D )
>> A && B && C && D
>
>No, you can't rely on equivalence of those commands, because
>in the first sample C and D are executed in a subshell.

I did wonder if I should raise that issue myself, but decided against it
since it seemed to be drifting away from what the OP asked about an
expression with four conditions.

If we're talking about assignments and other executable commands, then
yes, there's extra considerations when using the connective logic of
shell scripts.  e.g., an if-then-else structure can be made (in BASH at
least) with connective logic...

[[ 1 < 2 ]] && echo yes || echo no

.... which is fine provided that the commands are simple enough to be
sure that the command following the "&&" will exit as true, otherwise
the command following the "||" will execute irrespective of the initial
test condition.
```
 0
Dave
11/24/2015 3:09:48 PM
```On 24.11.2015 16:09, Dave Farrance wrote:
> Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>
>> On 24.11.2015 05:41, Hongyi Zhao wrote:
>>> Hi all,
>>>
>>> I've four conditions expression, A, B, C, and D.
>>>
>>> Are the following two forms equivalent or not?
>>>
>>> A && B && ( C && D )
>>> A && B && C && D
>>
>> No, you can't rely on equivalence of those commands, because
>> in the first sample C and D are executed in a subshell.
>
> I did wonder if I should raise that issue myself, but decided against it
> since it seemed to be drifting away from what the OP asked about an
> expression with four conditions.

Mind, we were talking about a conditional command list in shell context
(not any other language, not other shell contexts). If the OP would have
arithmetic contexts in mind he would (or at least should) have written

(( A && B && ( C && D ) ))

In conditional commands one should be aware that you can't put parenthesis
around commands while not recognising that these are a specific _subshell_
concept and not a _logical grouping_ as in arithmetic ((...)) expressions.

The previous posters did not consider that, and it required a correction

Janis

> [...]

```
 0
Janis
11/24/2015 3:37:49 PM
```On 2015-11-24, Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> wrote:
> Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
>>Are the following two forms equivalent or not?
>>
>>A && B && ( C && D )
>>A && B && C && D
>
> Yes. In either case, _all_ variables must be true for the expression to
> be true.

Parentheses have the side-effect of running commands in a subshell.
These two are not equivalent:

A && B && VAR=VALUE

A && (B && VAR=VALUE)

For that matter,

A && B && C

is neither equivalent to

(A && B) && C

nor to

A && (B && C)

> The AND operator is "associative". In this wiki page, find the
> subheading "Propositional logic", where "OR" is represented by a symbol
> that looks like "V", and "AND" by an inverted "V".

Don't confuse newbies with suggestions that the shell operators
are like propositional logic operators. Both && and || are on the same
precedence level, so that (in the absence of interaction between side effects
and subshells) these are equivalent:

A || B && C || D

((A || B) && C) || D

In logic notations, && has a higher precedence, so the parse is:

A || (B && C) || D

And, semantically, propositional logic operators do not denote sequencing
of imperative operations via short circuiting.
```
 0
Kaz
11/24/2015 4:09:41 PM
```On 2015-11-24, Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> wrote:
> Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> wrote:
>
>>The AND operator is "associative". In this wiki page, find the
>>subheading "Propositional logic", where "OR" is represented by a symbol
>>that looks like "V", and "AND" by an inverted "V".
>>
>>https://en.wikipedia.org/wiki/Associative_property
>
> That wiki page is for associativity generally, but I see there's also a
> page specifically about programming language operator associativity:
>
> https://en.wikipedia.org/wiki/Operator_associativity

These are not related. The associative property is a semantic concept which
tell us that if we combine three things in two different orders (without
swapping operand order---that requires commutativity), the result is the same.

Left-to-right operator associativity only tells us which way the parsing
ambiguity in "A op B op C" resolves, without any regard for what "op" means.

In a certain sense, the concepts are *opposite*.

Why? Because if op denotes a combinator which exhibits the associative
property, then op's associativity doesn't matter; it doesn't have to have it.
```
 0
Kaz
11/24/2015 4:21:45 PM

Similar Artilces:

Re: &&&&&&
Talbot, Its basic macro aper math. With each pass of the macro processor it reduces && to &. in yoru case: %put &&mlv&mlin. = &&&&&&mlv&mlin. ; 1st pass: &mlv5 = &&&mlv5 2nd Pass: Daisy = &Daisy 3rd Pass : Daisy = Yellow Toby Dunn From: Talbot Michael Katz <topkatz@MSN.COM> Reply-To: Talbot Michael Katz <topkatz@MSN.COM> To: SAS-L@LISTSERV.UGA.EDU Subject: &&&&&& Date: Thu, 9 Jun 2005 13:38:44 -0400 Received: from malibu.cc.uga.edu ([128.192.1.103]) by MC8-F21.hotmail...

& and &amp;
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved and redisplayed it is displayed as simply &. HTML snippet: <TEXTAREA NAME="p_html" ROWS=6 COLS=70 ALIGN="VIRTUAL" WRAP="YES"> fred & &amp; </TEXTAREA> When displayed, the user predictably sees fred & & What workarounds are there for this - I am sure it's a problem for others - is there a way...

&& and &
What exactly is the difference between && and & (|| and |)? When does it generates different results? Is the & wrong now from R14 on? Best regards Prom Prom wrote: > What exactly is the difference between && and & (|| and |)? When does > it generates different results? Is the & wrong now from R14 on? > > Best regards > Prom 'help &' shows: Logical operators. Short-circuit logical AND && Short-circuit logical OR || and - Element-wise logical AND &...

Re: &&&&&& #5
Toby, Fair to disagree but the approach I suggested works extremely well, is easy to debug, and has proven itself time after time. As the famous macro or not debate showed a few months back, there are lots of ways to handle code gen...macro is merely 1 way. The limit here is one of comfort: where does an individual programmer feel comfortable technology and approach-wise. If macro is it, then that is what you will tend to drift toward. If it is SCL, as Joe states, then it will be SCL. Personally, I use macros (to a point), %includes, c#, CodeSmith, and a host of other methods to gen code dep...

Re: &&&&&& #4
> From: Talbot Michael Katz > It requires SIX ampersands to resolve the macro variable > example below: > > data _null_ ; > set DS1 end = last ; > call symput("mlv" || compress(put(_N_,best32.)), mli) > ; > call symput(mli, tdt) ; > if last then do ; > call symput("mlin", compress(put(_N_,best32.))) ; > end ; > run ; > %put &&mlv&mlin. = &&&&&&mlv&mlin. ; > > (So, if there are five observations in the data set, and the > fifth value of > mli is "DAISY" and the fi...

Re: &&&&&& #13
Bold=Ian Whitlock Faint Italics=Joe Whitehurst A macro variable can be thought of as a name pointing at a value. A named scl list item can be thought of as a name pointing at a value. So a name can point at a name that can point at a value, etc. So a named scl list item can be thought of as a named scl list item that can point at a named scl list item that can point at a named scl list item that can point at a value which can continue to whatever level of abstraction you wish without ever using any concatenated ampersands. Let's look at the problem by analogy. Suppose you go to a pa...

Re: &&&&&& #11
Michael, Where were you when I needed you? Just how long have you kept that naughty question bottled up where it cannot see the light of day? How many others harbor that naughty question? Well, all of you got some answers today. Some technically perfect regurgitations of the SAS documentation without a care about consequences which those who don't like to read for themselves will find somewhat useful. Some sagely suggesting that some newer SAS Constructs might be worth investigating, and, indeed, there may be something worth examining about your approach to solving problems when you t...

Re: &&&&&& #2
Alan, I disagree with that statement. I think the best solution would be to build the dang thing the way he want with less macros. By sending your code out then back in is in MO to much hassel for too little. the whole thingy could be done like this: data one ; flower = 'Daisy' ; color = 'Yellow' ; run ; data two ; set one ; call symput ( compress('text'||_n_),compbl(flower||'='||color) ) ; call symput ( 'index' , compress(_n_) ) ; run ; %put &&text&index ; Toby Dunn From: Alan Churchill <SASL001@SAVIAN.NET> Reply-To: A...

Re: &&&&&& #9
"I have found that while in certain situations this may be the only way to accomplish something easily, it is over used and causes way more headaches than it is worth" My feeling on macros... Thanks, Alan Savian "Bridging SAS and Microsoft Technologies" -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of toby dunn Sent: Thursday, June 09, 2005 12:56 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: &&&&&& Alan, I seem to have started a hail storm here and I probably should have stated earlier that I don'...

Re: &&&&&& #8
On Thu, 9 Jun 2005 13:38:44 -0400, Talbot Michael Katz <topkatz@MSN.COM> wrote: >Hi. > >It requires SIX ampersands to resolve the macro variable example below: > >data _null_ ; > set DS1 end = last ; > call symput("mlv" || compress(put(_N_,best32.)), mli) > ; > call symput(mli, tdt) ; > if last then do ; > call symput("mlin", compress(put(_N_,best32.))) ; > end ; >run ; >%put &&mlv&mlin. = &&&&&&mlv&mlin. ; > >(So, if there are five observations in the data set, and the fifth va...

Re: &&&&&& #6
This is a prime example I use when I discuss what is wrong with the macro language. Yes, you can figure it out but an alternative would be to simply write your data step code out and then bring it back in so you know what is being submitted. data _null_ ; file 'c:\myfile.sas' ; put 'data ...;' ; ...more put statements... run; %include 'c:\myfile.sas' ; Others will argue for SCL or something else. Regardless, this application probably requires a complete rethink if you are using 6 ampersands. Thanks, Alan Savian "Bridging SAS and Microsoft Technologies...

Re: &&&&&& #10
Thank you, Toby et al for your answers, opinions, advice, criticism, etc. I almost think I'm beginning to get it. Let me regurgitate this to be sure: Given &&&&&&mlv&mlin., I resolve &mlin. = 5 and reduce the &&&&&& to &&& to get &&&mlv5. Then I resolve &mlv5. = DAISY and reduce the && to & to get &DAISY. Finally I resolve &DAISY. = YELLOW. So, it looks to me like once I reach a stage where I have a string of ampersands followed by a "pure" name (i.e., one with no ampersa...

Re: &&&&&& #12
Talbot, From another message you already seem satisfied with the information given about your problem, but I will add my penny thoughts for they give a different point of view. The basic question might be interpreted as, "Why does it take so many ampersands to express 'daisy=yellow'?" The implied question might be what is wrong with my structure and how should it be fixed? I am interested in both. A macro variable can be thought of as a name pointing at a value. So a name can point at a name that can point at a value, etc. Let's look at the problem by analogy. Suppo...

Re: &&&&&& #3
toby dunn <tobydunn@hotmail.com> replied: > Alan, > > I disagree with that statement. I think the best solution would be to build > the dang thing the way he want with less macros. By sending your code out > then back in is in MO to much hassel for too little. > > the whole thingy could be done like this: > > data one ; > flower = 'Daisy' ; > color = 'Yellow' ; > run ; > > data two ; > set one ; > call symput ( compress('text'||_n_),compbl(flower||'='||color) ) ; > call symput ( 'index' , compre...

Web resources about - A && B && ( C && D ) - comp.unix.shell

Resources last updated: 1/24/2016 11:28:02 PM