PT - Expressions and Assignment Statements (Lesson)
Expressions and Assignment Statements
Introduction
Now that we know what data types we can work with and how to declare and initialize them, let’s start manipulating them. We will work with variables and arithmetic operations to compute different results. As we try different examples, pay close attention to how variables and operators are sequenced. The way that they are combined in an expression will determine their calculated values and data type.
Assignment Statements
In the above statement we are declaring total as an int (integer) and initializing it to 123. Assignment operators are read from right to left so this means that 123 is assigned to total.
The assignment operator is the equal sign =. It does not mean the same as total equals 123. (We have a different operator for that.)
The above statement can be broken into two statements: declaration statement and initializing statement:
The first statement declares total as an int and the second statement initializes it to 123.
The assignment operator (=) assigns a new value to an identifier each time an initializing statement is written. If a new statement is written (see below) the value of total will change.
Now, total holds the value 47 and the old number, 123, has been thrown away.
Multiple variables can be declared and initialized at the same time.
When initializing more than one variable on a line, the variables must be the same type and use a comma in between each new variable.
Arithmetic Expressions Activity
Click below to start the Arithmetic Expressions Activity.
Division Practice Flashcards
Click "Continue" below to complete the Division Practice flashcard activity.
Click on "Runestone Academy" below to open the required reading that is listed.
Runestone Academy Links to an external site.: AP CSA – Java Review:
READ:
3.5 - Operators
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Introduction to Computer Science Using Java Links to an external site.:
READ:
Chapter 10: Expressions and Arithmetic Operators
Arithmetic Activity
Click below to answer the questions in the Arithmetic Activity.
Practice Activity: Problets – Arithmetic Expressions in Java
- Click on "Problets" below to open the website.
- Click on Topics.
- Look for title: "Expression Evaluation" and choose "Java" next to "Arithmetic Expressions" in the table.
This activity is interactive. It will add more practice problems the more you get wrong and fewer problems the more you get correct. It can take anywhere from 15-45 minutes to complete the activity.
Problets Links to an external site.
Please view the navigation example video below.
Escape Sequences
Have you noticed that when we want to print a sentence on the screen, we have to use quotations?
What happens if we want to print a statement that contains quotations? How can we print the actual quotations but still use them as the Java syntax that is required?
Enter escape sequences!
Escape sequences tell Java to break from what normally happens in order to output something different.
Output | Java code |
quotations (") | \" |
jump to a new line | \n |
tab | \t |
backslash (\) | \\ |
Notice that to escape from the Java norm we use a backsplash in front of what we want to print.
Type the following statements in the Dr. Java interactions pane to see what they print:
IMAGES CREATED BY GAVS