Java Reference
In-Depth Information
there are only two opening parentheses ( but three closing parentheses ), which is
an error. In the middle of an expression, the count of opening parentheses must be
greater than or equal to the count of closing parentheses, and at the end of the
expression the two counts must be the same.
Here is a simple trick to make the counting easier without using pencil and paper.
It is difficult for the brain to keep two counts simultaneously, so keep only one
count when scanning the expression. Start with 1 at the first opening parenthesis;
add 1 whenever you see an opening parenthesis; subtract 1 whenever you see a
closing parenthesis. Say the numbers aloud as you scan the expression. If the count
ever drops below zero, or if it is not zero at the end, the parentheses are
unbalanced. For example, when scanning the previous expression, you would
mutter
1.5 * (Math.sqrt(b * b - 4 * a * c) ) ) - ((b
/ 2 * a))
1 2 1 0 -1
and you would find the error.
Q UALITY T IP 4.3: White Space
The compiler does not care whether you write your entire program onto a single
line or place every symbol onto a separate line. The human reader, though, cares
very much. You should use blank lines to group your code visually into sections.
For example, you can signal to the reader that a n output prompt and the
corresponding input statement belong together by inserting a blank line before and
after the group. You will find many examples in the source code listings in this
topic.
152
153
White space inside expressions is also important. It is easier to read
x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
than
x1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
Search WWH ::




Custom Search