From the free course AP Computer Science A on Lykke
1.4 Assignment Statements and Input
Key concepts: Assignment operators and assignment statements · Expressions and typed values · Input and developing code to receive input · String manipulation and searching with indexOf() · Helper method calls for modular code · Conditional if-else if-else statements · Boolean expressions and comparisons · Incrementing variables in iterative logic · 2D arrays and grid-based movement · Toggling between two states
A program changes the world it models by assigning new values to variables and receiving values from outside the program. A temperature-monitoring program, for example, must read each temperature, store it, test it, and update a count when a heat wave continues.
1.4 Assignment Statements and Input
A program changes the world it models by assigning new values to variables and receiving values from outside the program. A temperature-monitoring program, for example, must read each temperature, store it, test it, and update a count when a heat wave continues.
Assignment: storing a new value
An assignment statement evaluates the expression on the right side of = and stores the resulting value in the variable on the left side. The old value is replaced.
int heatWaveLength = 0;
heatWaveLength = heatWaveLength + 1;
After the first statement, heatWaveLength is 0. The second statement evaluates heatWaveLength + 1 using the old value, produces 1, and then assigns 1 back to heatWaveLength.
Key rule: The right side is evaluated first; assignment happens afterward.
Compound assignment operators provide a shorter form for updating a variable:
heatWaveLength += 1; // equivalent to heatWaveLength = heatWaveLength + 1
total += temperature; // add temperature to total
count -= 1;
The AP course and exam do not require assignment operators to be embedded inside other expressions. Code such as a = b = 4; and a[i += 5] is outside the scope of AP Computer Science A. Keep assignment as its own clear step.
Expressions have typed results
An expression is code that is evaluated to produce one value with a specific type. For example, temperature > 90 produces a boolean, while temperature + 2 produces an int when temperature is an int. The resulting type must be compatible with the variable receiving it.
int temperature = 94;
boolean isHot = temperature > 90;
This connection between expressions and assignment is the core of Essential Knowledge 1.4.A.3: expressions are evaluated to produce a single value with a specific type. The assignment statement does not store the expression itself; it stores the value produced by evaluation.
Receiving input with Scanner
Learning Objective 1.4.B is to develop code to read input. Essential Knowledge 1.4.B.1 identifies the Scanner class as the primary AP method for obtaining text input from the keyboard.
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.print("Enter today's temperature: ");
int temperature = input.nextInt();
System.out.print("Enter a city: ");
input.nextLine(); // consume the remaining end-of-line
String city = input.nextLine();
nextInt() reads an integer token. nextLine() reads the remainder of the current line, so a nextLine() after nextInt() may first consume the leftover newline. Specific user-interface input, such as graphical buttons or menus, is outside the course and exam scope; console input through Scanner is the relevant model.
Strings, helper methods, and indexOf()
A helper method is a method called to perform a focused task for another method. In addSignature(String textStr), the call to getSignature() avoids duplicating the logic for obtaining the signature and keeps the method modular.
public String addSignature(String textStr)
{
String signature = getSignature();
int index = textStr.indexOf(signature);
if (index == -1)
{
return textStr + signature;
}
else if (index == 0)
{
return textStr;
}
else
{
return textStr.substring(0, index)
+ textStr.substring(index);
}
}
String.indexOf() returns the starting index of an existing sequence, or -1 when the sequence is absent. The if-else if-else structure handles three distinct cases: the signature is missing, it begins the text, or it occurs later. The final branch above preserves the text while making the three-case structure explicit; a real method specification may require a different transformation in that branch.
Conditions must compare complete values
A boolean expression must contain valid comparisons. This is invalid Java:
if (temperature >= 90 && < 100)
The second comparison must name both operands:
if (temperature >= 90 && temperature < 100)
{
System.out.println("Hot day");
}
When a heat-wave algorithm processes temperatures, assignment updates the measured state while conditionals decide what that state means:
if (temperature >= 90)
{
heatWaveLength++;
}
else
{
heatWaveLength = 0;
}
The variable measuring the current heat wave must be incremented during the wave and reset when the wave ends. If a longer wave is found, a separate variable must be updated to remember its length.
Misconception check
Misconception: two sequential if statements always behave like if-else. They do not. If the first statement changes the value tested by the second, both tests can succeed in one call. Use else when exactly one branch should execute.
if (active == 0)
{
active = 1;
}
else
{
active = 0;
}
Retrieval check
Suppose temperature is 95 and heatWaveLength is 2. After heatWaveLength += 1, what value is stored? Which value does indexOf() return when a signature is absent? Why is temperature >= 90 && < 100 invalid? The answers are `$3$, $-1$, and “the second comparison is incomplete.”

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A