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)