Название: OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119864592
isbn:
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 familiar with for
and for-each loops and know how to write and evaluate them. Each loop has its own special properties and structures. You should know how to use for-each loops to iterate over lists and arrays.
Understand how break, continue, and return can change flow control. Know how to change the flow control within a statement by applying a break
, continue
, or return
statement. Also know which control statements can accept break
statements and which can accept continue
statements. Finally, you should understand how these statements work inside embedded loops or switch
statements.