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

Unit 3 · Heavy

Repetition

Doing something many times without writing it many times. Most early loop bugs are off by exactly one, which is why counting the iterations deliberately matters more than the syntax.

What a strong answer looks like

A strong Unit 3 answer states the first value, the last value, and how many times the loop body runs.

Topics in this unit

1

Counting Loops

Practice this topic →

Know

A for loop sets a starting value, a condition to keep going, and a step. It checks the condition before each pass, so it may run zero times.

Apply

List the first and last values the counter takes before predicting anything.

Watch out

Going one step too far by using less-than-or-equal against a size.

Study move

For three loop headers, say exactly how many times each body runs.

2

Condition Loops

Practice this topic →

Know

A while loop repeats until its condition becomes false, and you are responsible for making that happen inside the body.

Apply

Check that every path through the body moves toward stopping.

Watch out

Forgetting the update, or putting it inside an if so it sometimes does not happen.

Study move

Write a while loop that ends, then remove the update and predict what happens.

3

Loops Inside Loops

Practice this topic →

Know

The inner loop finishes completely for every single pass of the outer one, so the total work multiplies.

Apply

Say what each loop is counting before you trace.

Watch out

Using the outer counter inside the inner loop by accident.

Study move

Predict the number of lines printed by a nested loop before running it.

4

Totals and Counters

Practice this topic →

Know

A running total must start at a value that is correct when nothing has been added yet: zero for a sum, one for a product.

Apply

Say in words what the total holds after one pass, then check the starting value fits.

Watch out

Starting a running largest-value at zero, which breaks when every value is negative.

Study move

Find the largest value in a list and explain why the first element is the safe starting point.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Counting iterations deliberately
  • Making sure a while loop can end
  • Choosing a correct starting value for a total

Varies by course

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

  • do-while. Covered in some sections.
  • break and continue. Introduced at different points and sometimes discouraged early.

Mastery checklist

  • State how many times a given loop runs.
  • Explain why a while loop never ends.
  • Predict the total passes of a nested loop.
  • Choose starting values for a sum and a largest-so-far.

Check yourself

  • When does a for loop run zero times?
  • What makes a while loop infinite?
  • Why is zero a bad start for a largest-value tracker?

Modeling drill

Write a loop that adds up only the even numbers from 1 to 20 and say what it produces if the range is empty.

LoopCounterConditionIterationNested loopRunning totalOff by one