• does an expression in a logical statement need parenthesis for proper e

    From Lynn McGuire@3:633/10 to All on Thu Nov 5 17:42:54 2026
    Subject: does an expression in a logical statement need parenthesis for proper evaluation?

    Hi,

    Does an expression in a logical statement need parenthesis for proper evaluation in C and/or C++?

    In other words, should

    if (j1 * i1 < 1) {
    goto L463;
    }

    be

    if ((j1 * i1) < 1) {
    goto L463;
    }

    Thanks,
    Lynn


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Keith Thompson@3:633/10 to All on Thu Nov 5 16:33:18 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    Lynn McGuire <lynnmcguire5@gmail.com> writes:
    Does an expression in a logical statement need parenthesis for proper evaluation in C and/or C++?

    In other words, should

    if (j1 * i1 < 1) {
    goto L463;
    }

    be

    if ((j1 * i1) < 1) {
    goto L463;
    }

    No, but redundant parentheses are mostly harmless.

    The outer parentheses are of course part of the syntax of the "if"
    statement, but the operator precedences are unambiguous. `j1 * i1 < 1`
    can never be evaluated as `j1 * (i1 < 1)`.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Janis Papanagnou@3:633/10 to All on Fri Nov 6 01:40:48 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On 2026-11-06 00:42, Lynn McGuire wrote:
    Hi,

    Does an expression in a logical statement need parenthesis for proper evaluation in C and/or C++?

    No, below (assuming i1 and j1 are arithmetic types (and not used
    as "camouflaged" booleans) there's no parenthesis necessary.

    The necessity of parentheses depends on the operators used. In
    "C" the multiplication has higher precedence than the comparison.

    I suggest to get an Operator Precedence Table from the Net and
    inspect it until you got familiar with the operator precedences.
    Most precedences are anyway sensibly defined as you'd expect it.
    But be careful when using bit-expressions with '&', '|', and '^'
    especially if used together with the comparison operators.

    Janis


    In other words, should

    if (j1 * i1 < 1) {
    ÿÿÿ goto L463;
    }

    be

    if ((j1 * i1) < 1) {
    ÿÿÿ goto L463;
    }

    Thanks,
    Lynn



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Fri Nov 6 00:44:30 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On Thu, 5 Nov 2026 17:42:54 -0600, Lynn McGuire wrote:

    Does an expression in a logical statement need parenthesis for proper evaluation in C and/or C++?

    No <https://en.cppreference.com/c/language/operator_precedence>.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From James Kuyper@3:633/10 to All on Thu Nov 5 19:52:31 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On 2026-11-05 18:42, Lynn McGuire wrote:
    Hi,

    Does an expression in a logical statement need parenthesis for proper evaluation in C and/or C++?

    In other words, should

    if (j1 * i1 < 1) {
    goto L463;
    }

    be

    if ((j1 * i1) < 1) {
    goto L463;
    }
    No general statement can be made about that issue. It depends entirely
    upon the expression. The syntax rules of C (and the slightly different
    grammar rules of C++) specify what order the parts of the logical
    expression get evaluated in. If that happens to be the order that you
    want, no parentheses are needed. Otherwise, one or more parentheses
    might solve the problem.

    For the most part, the grammar was fairly well designed for the purpose
    of reducing the number of parentheses needed. For example, all
    arithmetic operators have higher precedence than the comparison
    operators, which in turn have higher priority than the logical
    operators. That is why the parentheses you added above are unnecessary.
    For historical reasons, bitwise or (|) and bitwise and (&) are
    exceptions. Unfortunately, correcting that mistake would break backwards compatibility, which is a fairly high priority with both the C and C++ committees.

    It never hurts to insert parentheses to specify precisely the order of evaluation that you want - except insofar as other people reading your
    code who know that a given set of parentheses are unnecessary might
    guess that you don't. If you don't want them to do that, take the time
    to learn the grammar rules.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lynn McGuire@3:633/10 to All on Thu Nov 5 19:07:46 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On 11/5/2026 5:42 PM, Lynn McGuire wrote:
    Hi,

    Does an expression in a logical statement need parenthesis for proper evaluation in C and/or C++?

    In other words, should

    if (j1 * i1 < 1) {
    ÿÿÿ goto L463;
    }

    be

    if ((j1 * i1) < 1) {
    ÿÿÿ goto L463;
    }

    Thanks,
    Lynn

    Thank you all very much. BTW, there were parenthesis in the old Fortran
    code but F2C stripped them out. I put them back in for my comfort as I
    see them.

    Lynn


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Fri Nov 6 01:12:15 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On Thu, 5 Nov 2026 19:52:31 -0500, James Kuyper wrote:

    For historical reasons, bitwise or (|) and bitwise and (&) are
    exceptions. Unfortunately, correcting that mistake would break
    backwards compatibility ...

    The only real issue is that bitwise ops have lower priority than
    comparisons, when they should be higher. If they were higher, then an expression like

    (bitval & mask) == desired_value

    wouldn?t need parentheses.

    Realistically, can anyone offer *any* real-world C code that
    uses an expression that would be parenthesized like

    a & (b == c)

    ? The reason why I ask is that Python makes exactly this change to the
    operator precedences, and I have yet to come across anybody complain
    that they converted some expression from C to Python, and it came up
    with the wrong answer because of this issue.

    In fact, I only discovered for myself that Python did the precedence differently because I mistakenly wrote some code without the usual C-precedence-compatible parentheses

    bitval & mask == desired_value

    and got the right answer anyway. ;)

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Keith Thompson@3:633/10 to All on Thu Nov 5 21:35:02 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    James Kuyper <jameskuyper@alumni.caltech.edu> writes:
    [...]
    Otherwise, one or more parentheses
    might solve the problem.

    Technically correct (the best kind of correct), but *two* or more
    parentheses are even better.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Tim Rentsch@3:633/10 to All on Thu Nov 5 21:40:45 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

    James Kuyper <jameskuyper@alumni.caltech.edu> writes:
    [...]

    Otherwise, one or more parentheses
    might solve the problem.

    Technically correct (the best kind of correct), but *two* or more
    parentheses are even better.

    Wouldn't it be better still to say one or more pairs of
    parentheses?

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Nov 6 08:49:15 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On 06/11/2026 02:07, Lynn McGuire wrote:
    On 11/5/2026 5:42 PM, Lynn McGuire wrote:
    Hi,

    Does an expression in a logical statement need parenthesis for proper
    evaluation in C and/or C++?

    In other words, should

    if (j1 * i1 < 1) {
    ÿÿÿÿ goto L463;
    }

    be

    if ((j1 * i1) < 1) {
    ÿÿÿÿ goto L463;
    }

    Thanks,
    Lynn

    Thank you all very much.ÿ BTW, there were parenthesis in the old Fortran code but F2C stripped them out.ÿ I put them back in for my comfort as I
    see them.


    Since you are generating the C++ code automatically, there should not be
    a problem adding the parentheses. Generated code that contains "goto
    L463" is clearly not intended for humans anyway (at least, not without a
    good deal of effort).

    There is a proportion of programmers who think it is somehow blasphemy
    to use more parentheses in an expression than strictly necessary. That
    type of programmer will also disapprove of the extra braces around the
    "goto" in your example.

    There is a proportion of programmers who think you should /always/ use parentheses to show the precedence of operators.

    Most programmers, I think, are in the middle. Parentheses here can make
    the expression clearer at a glance, but it is also obvious what it means
    when not using extra parentheses. It is common to have parentheses in situations where the operator precedence is not as well known, or may
    differ between programming languages - mixing bitwise and logical
    operators and equality operators are examples.

    You can get a reasonable balance using gcc's warnings - these also have
    the big advantage of making it easy to check! "-Wparentheses" (enabled
    by -Wall) will catch, for example, "a < b == c" which parses as "a < (b
    == c)". If "-Wparentheses" does not lead to complaints, then your
    generated code is probably okay without the extra parentheses - but you
    can still add them here if you prefer.

    In general, I think you want your generated code to pass "gcc -O2 -Wall" cleanly (or the clang equivalent, which is /not/ -Weverything!). You
    may even aim for -Wextra.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Janis Papanagnou@3:633/10 to All on Fri Nov 6 09:17:08 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On 2026-11-06 08:49, David Brown wrote:
    [...]

    There is a proportion of programmers who think it is somehow blasphemy
    to use more parentheses in an expression than strictly necessary.ÿ [...]

    "blasphemy", wow! - The exaggerations (despite being here, allegedly,
    softened by the word "somehow") seem to have become in vogue, lately.
    (Formerly I saw such argumentation paths prevalently in one specific
    poster's posts only. Communication style seems to be infectious. :-)

    Most (all?) professionals seem to prefer clearness as you do. Whatever
    it needs to obtain that; less parentheses or additional parentheses -
    depending on the expressions' complexity and context! - Largely still subjective anyway, but less of religious intensity, I'd say.

    Janis


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Nov 6 10:50:00 2026
    Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation?

    On 06/11/2026 09:17, Janis Papanagnou wrote:
    On 2026-11-06 08:49, David Brown wrote:
    [...]

    There is a proportion of programmers who think it is somehow blasphemy
    to use more parentheses in an expression than strictly necessary.ÿ [...]

    "blasphemy", wow! - The exaggerations (despite being here, allegedly, softened by the word "somehow") seem to have become in vogue, lately. (Formerly I saw such argumentation paths prevalently in one specific
    poster's posts only. Communication style seems to be infectious. :-)

    I thought the term "blasphemy" was so obviously an exaggeration that it
    could not be taken realistically. I intended to point out that there
    are extreme views in both directions regarding additional parentheses
    that do not change the meaning of expressions in the C language.


    Most (all?) professionals seem to prefer clearness as you do. Whatever
    it needs to obtain that; less parentheses or additional parentheses - depending on the expressions' complexity and context! - Largely still subjective anyway, but less of religious intensity, I'd say.


    Some people do treat these things with a surprising intensity. But it
    is more common to pretend and tongue-in-check exaggerate their importance.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)