Java Reference
In-Depth Information
Incorrect Number of Closing Braces
In the following code, you defi ne a function and then call it. However, there's a deliberate mistake. See if
you can spot where it is.
function myFunction()
{
x = 1;
y = 2;
if (x <= y)
{
if (x == y)
{
alert(“x equals y”);
}
}
myFunction();
This is why formatting your code is important — you'll have a much easier time spotting errors such
as this:
function myFunction()
{
x = 1;
y = 2;
if (x <= y)
{
if (x == y)
{
alert(“x equals y”);
}
}
myFunction();
Now you can see that the ending curly brace of the function is missing. When there are a lot of if, for,
or do while statements, it's easy to have too many or too few closing braces. This type of problem is
much easier to spot with formatted code.
Incorrect Number of Closing Parentheses
Take a look at the following code:
if (myVariable + 12) / myOtherVariable < myString.length)
Spot the mistake? The problem is the missing parenthesis at the beginning of the condition. You want
myVariable + 12 to be calculated before the division by myOtherVariable is calculated, so quite
rightly you know you need to put it in parentheses.
(myVariable + 12) / myOtherVariable
Search WWH ::




Custom Search