Chapter 5. Flow control

Exam objectives covered in this chapter

What you need to know

[3.3] Create if and if/else and ternary constructs. How to use if, if-else, if-else-if-else, and nested if constructs. The differences between using these if constructs with and without curly braces {}. How to use a ternary construct. How it compares with if and if-else constructs.
[3.4] Use a switch statement. How to use a switch statement by passing the correct type of arguments to the switch statement and case and default labels. The change in the code flow when break and return statements are used in the switch statement.
[5.1] Create and use while loops. How to use the while loop, including determining when to apply the while loop.
[5.2] Create and use for loops including the enhanced for loop. How to use for and enhanced for loops. The advantages and disadvantages of the for loop and enhanced for loop. Scenarios when you may not be able to use the enhanced for loop.
[5.3] Create and use do/while loops. Creation and use of do-while loops. Every do-while loop executes at least once, even if its condition evaluates to false for the first iteration.
[5.4] Compare loop constructs. The differences and similarities between for, enhanced for, do-while, and while loops. Given a scenario or a code snippet, knowing which is the most appropriate loop.
[5.5] Use break and continue. The use of break and continue statements. A break statement can be used within loops and switch statements. A continue statement can be used only within loops. The difference in the code flow when a break or continue statement is used. Identify the right scenarios for using break and continue statements.

We all make multiple decisions on a daily basis, and we often have to choose from a number of available options to make each decision. These decisions range from the complex, such as selecting what subjects to study in school or which profession to choose, to the simple, such as what food to eat or what clothes to wear. The option you choose can potentially change the course of your life, in a small or big way. For example, if you choose to study medicine at a university, you may become a research scientist; if you choose fine arts, you may become a painter. But deciding whether to eat pasta or pizza for dinner isn’t likely to have a huge impact on your life.

You may also repeat particular sets of actions. These actions can range from eating an ice cream cone every day, to phoning a friend until you connect, to passing exams at school or university in order to achieve a desired degree. These repetitions can also change the course of your life: you might relish having ice cream every day or enjoy the benefits that come from earning a higher degree.

In Java, the selection statements (if and switch) and looping statements (for, enhanced for, while, and do-while) are used to define and choose among different courses of action, as well as to repeat lines of code. You use these types of statements to define the flow of control in code.

In the OCA Java SE 8 Programmer I exam, you’ll be asked how to define and control the flow in your code. To prepare you, I’ll cover the following topics in this chapter:

  • Creating and using if, if-else, ternary, and switch constructs to execute statements selectively
  • Creating and using loops: while, do-while, for, and enhanced for
  • Creating nested constructs for selection and iteration statements
  • Comparing the do-while, while, for, and enhanced for loop constructs
  • Using break and continue statements

In Java, you can execute your code conditionally by using either the if or switch construct. Let’s start with the if construct.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238