Java Reference
In-Depth Information
The exam tests your knowledge of all aspects of fl ow control,
including decision making, loop control structures, assertions, and
exception handling. This chapter covers all of these topics in detail.
Overview of Flow Control
Flow control refers to the order in which the statements in your Java program execute.
The starting point of a Java program is the main method, and the statements of your Java
program generally execute in the order they appear. However, we often need to alter
this fl ow of control by making decisions or looping through statements to repeat a task.
Problems might arise at runtime that might justify a method immediately terminating, or
you might have trouble fi nding a bug so you make various assertions in your code. Each
of these situations changes the order of execution (and therefore the fl ow of control) of the
statements in your program.
Section 2 of the SCJP exam tests your knowledge of the various aspects of Java that
affect the fl ow of control of a Java program. For example, Java contains the following
typical control structures that most programming languages defi ne for making decisions
and repetition:
Decision Making: The if-else and switch statements are the two control structures
in Java for making decisions.
Repetition: for loops, enhanced for loops, while loops, and do-while loops are the
control structure for performing repetition.
This chapter discusses the proper syntax and usage of these control structures. We also
examine the details of Java assertions, which are helpful in detecting and fi xing bugs in your
Java programs. In addition, we cover exception handling in detail, including when exceptions
need to be caught and when they can be ignored. We start with the control structures,
beginning with the most basic of decision-making structures: the if-else statement.
The if Statement
The exam objectives state that you should be able to “develop code that implements an if
statement and identify legal argument types.” An if statement , also referred to as an if-
else or if-then-else statement, is the most basic of decision-making control structures in
Java. Figure 3.1 shows the syntax of an if statement.
Search WWH ::




Custom Search