Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Precedence Misunderstanding

Reply
Thread Tools

Precedence Misunderstanding

 
 
Nick Keighley
Guest
Posts: n/a
 
      07-19-2012
On Jul 18, 7:29*pm, Rui Maciel <(E-Mail Removed)> wrote:

> Algebra is a branch of mathematics which covers operators and relations
> between sets. *These sets aren't limited to the real numbers set, and the
> operators aren't limited to the basic mathematical operators that take real
> values as operands. *In fact, typically the first contact with algebra tends
> not to involve real numbers at all, or basic math operators. *Take, for
> example, how Venn diagrams tend to be presented in kindergarten.


and so?

> Regarding "side effects", the concepts which I've referred to are indeed
> covered, and they tend to be covered right in the very first explanation of
> what a function is supposed to be. *Once you were taught that a function f
> maps elements in a set A to elements in a set B, you covered all the math
> that is needed to explain and understand "side effects". *No new math is
> needed to describe a function assigning a certain value to a flag, or
> returning a different value depending on the value stored in any other flag.


maths can be used to model almost anything. Including exotic
things like functions with side effects; this doesn't mean basic
mathematics covers side effects in any obvious or intuitive sense.
Anyone whose glanced at formal methods (the maths of programming)
would quickly see this.

There are also functional programming languages that go to great
lengths (cf. Haskell monads) to avoid side effects (so far as they
can) precisely because side effect free programs are easier to "reason
about" (analyse mathematically).
 
Reply With Quote
 
 
 
 
Phil Carmody
Guest
Posts: n/a
 
      07-19-2012
"BartC" <(E-Mail Removed)> writes:

> "Ike Naar" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
> > On 2012-07-18, Philip Lantz <(E-Mail Removed)> wrote:
> >> Ben Bacarisse wrote:
> >> [Referring to an expression containing only && and || operators]
> >>> an operand that is further to the right is never evaluated before one
> >>> to its left.
> >>
> >> Except when one to its left doesn't need to be evaluated.
> >>
> >> For example, in
> >> (a || b) && c
> >> with a = 1, a is evaluated, b is not evaluated, and c is evaluated.

> >
> > It's not a counterexample to what Ben said.
> > Even in your example, no operand is evaluated before one to its left.

>
> How about:
>
> a && 0
>
> or:
>
> a || 1
>
> a needn't be evaluated; but it is.


As if...

Phil
--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
 
 
 
Phil Carmody
Guest
Posts: n/a
 
      07-19-2012
Willem <(E-Mail Removed)> writes:
> Charles Richmond wrote:
> ) The crux of what I do *not* understand is this:
> )
> ) Can someone explain how "&&" can have a *higher* precedence than
> ) "||"... when any series of AND's and OR's are just going to be
> ) done left to right???
>
> The precedence determines how the results *will be used*.
> It does not determine the order in which they are evaluated.
>
> ) For example, if you have a long series of logical AND's and OR's like this:
> )
> ) a && b || c || d && x && y || z
> )
> ) How will "&&" having a higher precedence than "||" effect this at all???
>
> This is, step by step, what the program will do:
>
> /* Construct for the expression a && b || c || d && x && y || z */
> int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
> {
> if (a) {
> if (b) {
> return 1;
> }
> }
> if (c) {
> return 1;
> }
> if (d) {
> if (x) {
> if (y) {
> return 1;
> }
> }
> }
> if (z) {
> return 1;
> }
> return 0;
> }
>
> Now, as you can see: a, b, c, d, x, y, z are evaluated in that order.


It's possible to evaluate c when you've not evaluated b. There may be
a weak ordering, but I'm not sure a non-mathematical reading would
include that as an interpretation.

> Suppose that '&&' had higher precedence, it would become the following:


s/&&/||/

Phil
--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      07-19-2012
Phil Carmody wrote:
) Willem <(E-Mail Removed)> writes:
)> This is, step by step, what the program will do:
)>
)> /* Construct for the expression a && b || c || d && x && y || z */
)> int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
)> {
)> if (a) {
)> if (b) {
)> return 1;
)> }
)> }
)> if (c) {
)> return 1;
)> }
)> if (d) {
)> if (x) {
)> if (y) {
)> return 1;
)> }
)> }
)> }
)> if (z) {
)> return 1;
)> }
)> return 0;
)> }
)>
)> Now, as you can see: a, b, c, d, x, y, z are evaluated in that order.
)
) It's possible to evaluate c when you've not evaluated b. There may be
) a weak ordering, but I'm not sure a non-mathematical reading would
) include that as an interpretation.

Yes, I wrote that piece of code to demonstrate exactly what will
happen, precisely because english wording tends to be so ambiguous.

You can either try to precisify the wording, or you can write in code.

)> Suppose that '&&' had higher precedence, it would become the following:
)
) s/&&/||/

Good catch. Or s/higher/lower/, of course.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vec - a misunderstanding Mark Hobley Perl 1 03-18-2006 07:32 PM
Misunderstanding ConnectionStrings? Shawn Wildermuth ASP .Net 6 01-11-2006 02:57 AM
View State Misunderstanding Philip Tripp ASP .Net 3 07-26-2004 04:13 PM
errors in book or my misunderstanding? David K MCSE 4 12-12-2003 03:22 PM
Re: Class property misunderstanding Kevin Spencer ASP .Net 1 06-26-2003 05:28 PM



Advertisments