Introduction
to Interactive Programming
by Lynn Andrea
Stein
A Rethinking
CS101 Project
Java statements are executable code with neither type nor value. A Java statement is made up of exactly one of the following (though its pieces sometimes contain other statements).
This is an empty statement. Executing it has no effect. Execution continues immediately after this statement. | |
This is a simple statement. expression must be a side-effecting expression, i.e., an assignment, autoincrement, autodecrement, method invocation, or new expression. This kind of statement simply evaluates the expression and terminates. Execution continues immediately after this statement. | |
This is a block. statements is simply a sequence of zero or more statements, one after the other. This kind of statement executes by executing each statement in statements, in order. After it completes, execution continues immediately after the closing brace. | |
type name ; 6 |
This is a simple declaration. A declaration statement is executed by creating an association between the name name and the type type for the remainder of the execution of the enclosing block. Execution continues immediately after this statement. |
type name = value ; 6 |
This is a definition -- a declaration coupled with an intialization assigment. The execution of a definition is identical to the execution of a declaration except that the value associated with name is initially set to be value. |
Executing a return statement has the effect of exiting the innermost enclosing method. If optionalExpression is present, it is evaluated and its value is returned by the method invocation. | |
A synchronized statement executes block. However, execution cannot begin until the exclusive lock on the object whose value is objExpression is held by the Thread executing the synchronized statement. | |
This is a simple conditional statement. booleanExpression is an expression whose type is boolean. statement is any statement, often a block. A conditional statement is executed by first evaluating booleanExpression. If its value is true, statement is executed. If the value of booleanExpression is false, statement is not executed. In either case, execution continues immediately after this statement. | |
This is another form of conditional statement. booleanExpression is an expression whose type is boolean. Both statement and anotherStatement are arbitrary statements. Often statement is a block and anotherStatement is another conditional statement A conditional statement is executed by first evaluating booleanExpression. There are two possibilities 1. If the value of booleanExpression is true, statement is executed, then execution continues after anotherStatement. 2. If booleanExpression. is false, execution skips statement and continues at anotherStatement. | |
switch (
integralExpression )
|
A switch statement is a limited contditional. integralExpression is an expression with integral type (i.e., byte, short, int, long, boolean, or char). Each integralValues is a constant expression of the same integral type. Any number of case integralValue : and statement lines, and at most one default:, can be intermixed in any order in the switch body. A switch statement evaluates integralExpression and compares its valuewith the various integralValues. When the first matching integralValue is encountered, the remainder of the statements in the switch body are executed. A default: matches any value. Frequently, a break statement is used to exit the switch body. Typically, the format will be one or more case lines followed by one or more statements, often terminating in a break statement. This structure will then repeat. The final set of statements will often be preceded by default: |
This is a while loop. booleanExpression is an expression whose type is boolean. statement is any statement, frequently a block. A while loop is executed by first evaluating booleanExpression. If its value is false, execution continues after the while statement. Otherwise, statement is executed and then the while loop is executed again from the beginning. | |
This is a do loop. statement is any statement, frequently a block. booleanExpression is an expression whose type is boolean. A do loop is executed by first executing statement. Next, booleanExpression. is evaluated. If its value is false, execution continues after the do loop. Otherwise the do loop is executed again from the beginning. | |
This is a for loop. initExprs is a comma-separated sequence of expressions, typically one or more definitions or other initializations. booleanExpr is a single expression of type boolean. updateExprs is a comma-separated sequence of expressions, typically assignments or other updates. To execute a for loop, first evaluate initExprs. These expressions are evaluated only once, at the beginning of the for loop. Now evaluate booleanExpr. If its value is false, the for loop is complete; execution resumes after the entire statement. Otherwise, execute statement. Next, evaluate updateExprs. Regardless of its value, return to the beginning of this paragraph (evaluate booleanExprs). | |
This is a labeled statement. label may be any Java identifier. It does not interfere with variable scoping, etc. statement may be any statement, but is often a block or block-containing statement. A labeled statement is executed exactly as though label were not present, except that the position within the code of the identifier label is recorded. In particular, any break or continue statements appearing inside statement may make reference to label. (See break and continue statements in this chart.) | |
If optionalLabel is present, this is a labeled break statement; if it is absent, this is an unlabeled break statement. A labeled break statement exits all enclosing blocks until one whose label matches optionalLabel is found. Execution resumes after that block. An unlabeled break statement exits the innermost switch, while, do, or for statement containing that break. Execution resumes after that block. | |
The syntax of a continue statement is identical to that of a break statement. A continue statement may only appear inside a while, do, or for loop. An unlabeled continue statement exits the current iteration of the innermost loop containing that continue statement. Execution resumes at the expression evaluation (test) of while and do loops, and at the update expression of a for loop. A labeled continue statement exits the current iteration of the enclosing loop whose label matches optionalLabel. Execution resumes at the expression evaluation (test) of while and do loops, and at the update expression of a for loop. | |
Executing a throw statement has the effect of exiting every enclosing statement (including those containing method invocations) until a try statement with a catch clause matching the thrown object (the value of expression) is encountered. Execution resumes at this catch clause. | |
try |
This is a try statement. tryBlock, catchBlock, and finallyBlock are each arbitrary blocks of statements. type is any type that implements Throwable; name is any Java name. The lines finally finallyBlock are optional and may be omitted entirely. The lines catch ( type name ) catchBlock form a catch clause. Each try statement may have zero or more catch clauses (but exactly one try and at most one finally). To execute a try statement, tryBlock is executed. There are three possibilities: 1. If the execution of tryBlock completes normally, then execution continues at finallyBlock (if present). 2. If a throw statement is encountered during the execution of tryBlock and the type of the thrown object matches thetype of one of the catch clauses, execution exitstryBlock and continues in the catchBlock of the first catch clause whose type matches the type of the thrown object. During execution of catchBlock, name is bound to the thrown object. After completing this catchBlock, execution continues at finallyBlock (if present). 3. If execution of tryBlock is interrupted for any other reason -- a thrown object that doesn't match a catch clause, a break or continue statement -- execution exitstryBlock and continues at finallyBlock (if present). If execution of finallyBlock completes normally (or if finallyBlock is absent, execution continues abnormally, i.e., as though finallyBlock had thrown the object or raised the break or continue. That is, execution continues exiting blocks until the throw, break, or continue is resolved. |
© 1999 Lynn Andrea Stein
This chapter is excerpted from a draft ofIntroduction to Interactive Programming In Java, a forthcoming textbook from Morgan Kaufmann Publishers, Inc. It is a part of the course materials developed as a part of Lynn Andrea Stein's Rethinking CS101 project at the MIT AI Lab and the Department of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology.
Questions or comments:
<cs101-webmaster@ai.mit.edu>