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-else
statement, aswitch
statement, exception-handling statements, and branching statements.
Use thewhile
statement 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
-while
statement 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
-while
block execute at least once:do { statement(s) } while (expression);The
for
statement 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
-else
andswitch
. The more general-purpose statement isif
; useswitch
to make multiple-choice decisions based on a single integer value. The following is the most basicif
statement whose single statement block is executed if the boolean expression istrue
:Here's anif (boolean expression) { statement(s) }if
statement with a companionelse
statement. Theif
statement 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 if
to construct compoundif
statements:Theif (boolean expression) { statement(s) } else if (boolean expression) { statement(s) } else if (boolean expression) { statement(s) } else { statement(s) }switch
statement evaluates an integer expression and executes the appropriatecase
statement.switch (integer expression) { case integer expression: statement(s) break; ... default: statement(s) break; }
Use thetry
,catch
, andfinally
statements to handle exceptions.try { 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;break
statement to terminate the innermostswitch
,for
,while
, ordo
-while
statement.Use the labeled form of thebreak;break
statement to terminate an outerswitch
,for
,while
, ordo
-while
statement with the given label:Abreak label;continue
statement terminates the current iteration of the innermost loop and evaluates the boolean expression that controls the loop.The labeled form of thecontinue;continue
statement terminates the current iteration of the loop with the given label:Usecontinue label;return
to terminate the current method.You can return a value to the method's caller, by using the form ofreturn;return
that takes a value.return value;
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.