CHAPTER 43 — More Ways to Make Decisions

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

created: 11/07/98; revised: 11/24/99, 01/22/00, 07/20/02, 05/26/03, 01/28/06

CHAPTER 43 — More Ways to Make Decisions

Nested if and else statements can be used to choose between several options. But, sometimes it is inconvenient to use nested statements. This chapter discusses other statements that choose between several options.

Chapter Topics:

  • The conditional operator ?
  • The switch statement

Neither of these statements does anything that can't be done with ifs and elses. But they are useful enough to know about, and you will need to know them to use programs written by other programmers.

Neither of these statements are part of the AP Computer Science test given to high school students in the US. For them, this entire chapter may be skipped. These statements are not used in the chapters that follow.

QUESTION 1:

(Math Review:)

  • What is the absolute value of -9?
  • What is the absolute value of +9?