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.

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.

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.

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.

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.

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.

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

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