π1.4: Assignment & Input
Table of Contents
π This page is a condensed version of CSAwesome Topic 1.4
Assignment Statements
Assignment statements initialize or change the value stored in a variable using the assignment operator =
. An assignment statement always has a single variable on the left-hand side. The value of the expression on the right is assigned to and stored in the variable on the left.
Instead of saying βequalsβ for the =
, say βgetsβ or βis assignedβ to remember that the variable stores the value on the right.
Here, the variable
score
is assigned the value of10 * points + 5
.
You can also set one variableβs value to a copy of the value of another variable:
y = x;
This does not change the value of the original variable.
Predict: what will be printed after running this code?
int x = 0;
int y = 1;
int z = 2;
x = y;
y = y * 2;
z = 3;
System.out.println(x);
System.out.println(y);
System.out.println(z);
Data Types in Assignments
Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type.
The code below will cause a type mismatch error. Change one variableβs type so it works, then run it and confirm the output.
int x = 1;
double y = 2.2;
x = 2 * y;
System.out.println(x);
Incrementing & Decrementing
If you use a variable to keep score, you would probably increment it (add on to the current value) whenever score should go up. You can do this by setting the variable to the current value of the variable plus one (score = score + 1
) as shown below.
The formula would look strange in math class, but it makes sense in coding because it is assigning a new value to the variable on the left that comes from evaluating the arithmetic expression on the right.
To increment (increase an existing value):
score = score + 1;
To decrement (decrease an existing value):
score = score - 1;
Since changing the value of a variable by one is especially common, there are two extra concise operators ++
and --
, also called the plus-plus or increment operator and minus-minus or decrement operator that set a variable to one greater or less than its current value.
Scanner
Input with Variables
Variables are a powerful abstraction in programming because the same algorithm can be used with different input values saved in variables.
Input can come in a variety of forms:
- Tactile for example by clicking on a button
- Audio with speech
- Visual using a webcam
- Or the most common form, text that the user types in.
π¨οΈ The Scanner
class in Java is one way to obtain text input from the keyboard.
Type and run this code. Try it with different names.
import java.util.Scanner; // IMPORT SCANNER AT TOP
public class Main
{
public static void main(String[] args)
{
System.out.println("Please type in a name in the input box below.");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.println("Hello " + name);
scan.close();
}
}
The code above using the
Scanner
class will say hello to anyone who types in their name and will have different results for different name values.
Although you will not be tested in the AP CSA exam on using the Java input from the keyboard, learning how to handle input in Java is very useful and fun. For more information on using the Scanner
class, go to: W3Schools - Java User Input
Summary
-
(AP 1.4.A.2) The assignment operator (
=
) allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left. -
(AP 1.4.A.1) Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type.
-
(AP 1.4.A.1) A variable is initialized the first time it is assigned a value.
-
(AP 1.4.A.1) Reference types can be assigned a new object or
null
if there is no object. The literalnull
is a special value used to indicate that a reference is not associated with any object. -
(AP 1.4.A.3) During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the types of the values and operators used in the expression.
-
(AP 1.4.B.1) Input can come in a variety of forms, such as tactile, audio, visual, or text. The
Scanner
class is one way to obtain text input from the keyboard, although input from the keyboard will not be on the AP exam.
This lesson ends the section for the College Board Unit 1 Part 1.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.