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.

What a strong answer looks like

A strong Unit 2 answer checks the condition at its exact boundary rather than at a comfortable value in the middle.

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.

Study move

Write a condition with two comparisons and work out its value for four different inputs.

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.

Study move

Trace a grade classifier at 79, 80, 89, and 90.

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.

Study move

Write a check for an age between 13 and 19 and test both ends.

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.

Study move

Write a condition that checks something exists before looking inside it, then swap the order and explain the crash.

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.

Mastery checklist

  • Evaluate a two-part condition by hand.
  • Trace an if-else chain at its boundaries.
  • Write a range check that Java accepts.
  • Explain why check order matters.

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