Java Reference
In-Depth Information
If you had written the same code using the keyword void to ignore the result of the
expression (x + y).toString() , the parser would have added a semicolon after the
text z = Math.sqrt . The following code works as if you intended to assign the function
reference Math.sqrt to the variable named z :
var x = 200, y = 200, z // A semicolon is inserted here
z = Math.sqrt // A semicolon is inserted here
void (x + y).toString() // A semicolon is inserted here
print(z) // A semicolon is inserted here
function sqrt() { [native code] }
The reason that a semicolon was inserted at the end of the second line is that the
keyword void is an offending token in the third line. The keyword void cannot be part of
the assignment statement that started in the second line.
i advise using a semicolon as the statement terminator everywhere it is required.
Depending on the automatic insertion of semicolons may sometimes lead to subtle bugs.
Tip
I will discuss these statement types briefly in the following sections.
Block Statement
A block statement works similar to that of in Java. It is a group of zero or more statements
enclosed in braces ( { } ). Unlike Java, variables declared inside a block statement does
not have a local scope to that block. They can be accessed before and after the block
statement in which they are declared. The following snippet of code demonstrates this:
var empId = 100;
// Print empId and deptId. Note that deptId has not been
// declared yet, but you can access it.
print("empId = " + empId + ", deptId = " + deptId);
// A block statement
{
var deptId = 200;
print("empId = " + empId + ", deptId = " + deptId);
// Compute the area of a circle
var radius = 2.3;
var area = Math.PI * radius * radius;
printf("Radius = %.2f, Area = %.2f", radius, area);
}
 
 
Search WWH ::




Custom Search