Skip to main content
← Intro to CS (Java) Study Guide

Unit 2 · Steady

Making Decisions

Teaching a program to choose. The syntax is short; the difficulty is writing conditions that are true exactly when you meant them to be, especially at the edges of a range.

Topics in this unit

1

True and False Values

Practice this topic →

Know

A comparison produces true or false. And requires both parts true; or requires at least one; not flips the answer.

Apply

Work out each part separately, then combine them.

Watch out

Reading a condition as an English sentence rather than evaluating each comparison.

2

if and else

Practice this topic →

Know

An if-else chain runs at most one branch. Separate ifs can all run. The order matters when the ranges touch.

Apply

Check what happens at each boundary value, not just inside each range.

Watch out

Putting a wide condition first, so a later specific one can never be reached.

3

Combining Conditions

Practice this topic →

Know

A range check needs two comparisons joined together. Java will not accept the shorthand used in mathematics.

Apply

Write both comparisons explicitly and join them.

Watch out

Writing a chained comparison as you would on paper, which Java rejects or misreads.

4

Stopping Early

Practice this topic →

Know

Java stops checking a condition once the answer is settled, which lets an earlier check protect a later one.

Apply

Put the safety check first so the risky part is never reached when it would fail.

Watch out

Putting the checks in the wrong order, so the thing that crashes runs before the check meant to stop it.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • • Testing at boundary values
  • • Ordering branches from specific to general
  • • Using an early check as protection

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • switch. Taught in some sections as an alternative to long chains.
  • Ternary shorthand. Introduced in some courses for brevity.

Check yourself

  • • When can two separate if statements both run?
  • • Why does Java reject a chained comparison?
  • • How can an earlier check prevent a later crash?

Modeling drill

Write a program that reports whether a score is a pass, a merit, or a distinction, and check every boundary value.

booleanComparisonandornotBranchBoundary

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic