|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
For controlling the flow of a program, the Java programming language has three loop constructs, a flexibleif-elsestatement, aswitchstatement, exception-handling statements, and branching statements.
Use thewhilestatement to loop over a block of statements while a boolean expression remainstrue. The expression is evaluated at the top of the loop:while (boolean expression) { statement(s) }Use the
do-whilestatement to loop over a block of statements while a boolean expression remainstrue. The expression is evaluated at the bottom of the loop, so the statements within thedo-whileblock execute at least once:do { statement(s) } while (expression);The
forstatement loops over a block of statements and includes an initialization expression, a termination condition expression, and an increment expression:for (initialization ; termination ; increment) { statement(s) }
The Java programming language has two decision-making statements:if-elseandswitch. The more general-purpose statement isif; useswitchto make multiple-choice decisions based on a single integer value.The following is the most basic
ifstatement whose single statement block is executed if the boolean expression istrue:Here's anif (boolean expression) { statement(s) }ifstatement with a companionelsestatement. Theifstatement executes the first block if the boolean expression istrue; otherwise, it executes the second block:You can useif (boolean expression) { statement(s) } else { statement(s) }else ifto construct compoundifstatements:Theif (boolean expression) { statement(s) } else if (boolean expression) { statement(s) } else if (boolean expression) { statement(s) } else { statement(s) }switchstatement evaluates an integer expression or enum and executes the appropriatecasestatement.switch (integer expression) { case integer expression: statement(s) break; ... default: statement(s) break; }switch (expression of enum type) { case enum constant: statement(s) break; ... default: statement(s) break; }
Use thetry,catch, andfinallystatements to handle exceptions:Exception handling is covered in detail in the chapter Handling Errors with Exceptionstry { statement(s) } catch (exceptiontype name) { statement(s) } catch (exceptiontype name) { statement(s) } finally { statement(s) }.
Some branching statements change the flow of control in a program to a labeled statement. You label a statement by placing a legal identifier (the label) followed by a colon (:) before the statement:Use the unlabeled form of thestatementName: someJavaStatement;breakstatement to terminate the innermostswitch,for,while, ordo-whilestatement:Use the labeled form of thebreak;breakstatement to terminate an outerswitch,for,while, ordo-whilestatement with the given label:Abreak label;continuestatement terminates the current iteration of the innermost loop and evaluates the boolean expression that controls the loop:The labeled form of thecontinue;continuestatement skips the current iteration of the loop with the given label:Usecontinue label;returnto terminate the current method:You can return a value to the method's caller, by using the form ofreturn;returnthat takes a value:return value;
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.