Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
When you write a program, you type statements into a file. Without control flow statements, the interpreter executes these 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, theif
statement conditionally executes theSystem.out.println
statement within the braces, based on the return value ofCharacter.isUpperCase(aChar)
:The Java programming language provides several control flow statements, which are listed in the following table.char aChar; ... if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); }
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, you will see the following notation to describe the general form of a control flow statement:
Technically, the braces,control flow statement details { statement(s) }{
and}
, are not required if the block contains only one statement. However, we recommend that you always use{
and}
, because the code is easier to read and it helps to prevent errors when modifying code.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.