站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Control Flow Statements - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Control Flow Statements

When writing a program, you type statements into a file. Without control flow statements, the interpreter executes the statements in the order they appear in the file from left to right, top to bottom. You can use control flow statements in your programs to conditionally execute statements; to repeatedly execute a block of statements; and to otherwise change the normal, sequential flow of control. For example, in the following code snippet, the if statement conditionally executes the System.out.println statement within the braces based on the return value of Character.isUpperCase(aChar).
char aChar;
...
if (Character.isUpperCase(aChar)) {
    System.out.println("The character " + aChar
                       + " is uppercase.");
}
The Java programming language provides several control flow statements, which are listed in the following table.

Control Flow Statements
Statement Type Keyword
looping while, do-while, for
decision making if-else, switch-case
exception handling try-catch-finally, throw
branching break, continue, label:, return

In the sections that follow, the notation here is used to describe the general form of a control flow statement.

control flow statement details {
    statement(s)
}
Technically, braces — { and } — are not required if the block contains only one statement. However, we recommend that you always use the braces because the code is easier to read and they help prevent errors when modifying code.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.