OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
after them in the same block is considered unreachable and will not compile. For example, the following code snippet does not compile:
int checkDate = 0; while(checkDate<10) { checkDate++; if(checkDate>100) { break; checkDate++; // DOES NOT COMPILE } }
Even though it is not logically possible for the if
statement to evaluate to true
in this code sample, the compiler notices that you have statements immediately following the break
and will fail to compile with “unreachable code” as the reason. The same is true for continue
and return
statements, as shown in the following two examples:
int minute = 1; WATCH: while(minute>2) { if(minute++>2) { continue WATCH; System.out.print(minute); // DOES NOT COMPILE } } int hour = 2; switch(hour) { case 1: return; hour++; // DOES NOT COMPILE case 2: }
One thing to remember is that it does not matter if the loop or decision structure actually visits the line of code. For example, the loop could execute zero or infinite times at runtime. Regardless of execution, the compiler will report an error if it finds any code it deems unreachable, in this case any statements immediately following a break
, continue
, or return
statement.
Reviewing Branching
We conclude this section with Table 3.1, which will help remind you when labels, break
, and continue
statements are permitted in Java. Although for illustrative purposes our examples use these statements in nested loops, they can be used inside single loops as well.
TABLE 3.1 Control statement usage
Support labels |
Support break
|
Support continue
|
Support yield
|
|
---|---|---|---|---|
while
|
Yes | Yes | Yes | No |
do/while
|
Yes | Yes | Yes | No |
for
|
Yes | Yes | Yes | No |
switch
|
Yes | Yes | No | Yes |
Last but not least, all testing centers should offer some form of scrap paper or dry-erase board to use during the exam. We strongly recommend you make use of these testing aids, should you encounter complex questions involving nested loops and branching statements.
Summary
This chapter presented how to make intelligent decisions in Java. We covered basic decision-making constructs such as if
, else
, and switch
statements and showed how to use them to change the path of the process at runtime. We also presented newer features in the Java language, including pattern matching and switch
expressions, both designed to reduce boilerplate code.
We then moved our discussion to repetition control structures, starting with while
and do
/while
loops. We showed how to use them to create processes that loop multiple times and also showed how it is important to make sure they eventually terminate. Remember that most of these structures require the evaluation of a particular boolean
expression to complete.
Next, we covered the extremely convenient repetition control structures: the for
and for-each loops. While their syntax is more complex than the traditional while
or do
/while
loops, they are extremely useful in everyday coding and allow you to create complex expressions in a single line of code. With a for-each loop, you don't need to explicitly write a boolean
expression, since the compiler builds one for you. For clarity, we referred to an enhanced for
loop as a for-each loop, but syntactically both are written using the for
keyword.
We concluded this chapter by discussing advanced control options and how flow can be enhanced through nested loops coupled with break
, continue
, and return
statements. Be wary of questions on the exam that use nested loops, especially ones with labels, and verify that they are being used correctly.
This chapter is especially important because at least one component of this chapter will likely appear in every exam question with sample code. Many of the questions on the exam focus on proper syntactic use of the structures, as they will be a large source of questions that end in “Does not compile.” You should be able to answer all of the review questions correctly or fully understand those that you answered incorrectly before moving on to later chapters.
Exam Essentials
Understand if and else decision control statements. The if
and else
statements come up frequently throughout the exam in questions unrelated to decision control, so make sure you fully understand these basic building blocks of Java.
Apply pattern matching and flow scoping. Pattern matching can be used to reduce boilerplate code involving an if
statement, instanceof
operator, and cast operation using a pattern variable. It can also include a pattern or filter after the pattern variable declaration. Pattern matching uses flow scoping in which the pattern variable is in scope as long as the compiler can definitively determine its type.
Understand switch statements and their proper usage. You should be able to spot a poorly formed switch
statement on the exam. The switch
value and data type should be compatible with the case
statements, and the values for the case
statements must evaluate to compile-time constants. Finally, at runtime, a switch
statement branches to the first matching case
, or default
if there is no match, or exits entirely if there is no match and no default
branch. The process then continues into any proceeding case
or default
statements until a break
or return
statement is reached.
Use switch expressions correctly. Discern the differences between switch
expressions and switch
statements. Understand how to write switch
expressions correctly, including proper use of semicolons, writing case
expressions and blocks that yield a consistent value, and making sure all possible values of the switch
variable are handled by the switch expression.
Write while loops. Know the syntactical structure of all while
and do
/while
loops. In particular, know when to use one versus the other.
Be able to use for loops. You should be