3. Assignment

3.1. Objectives

  • Assign values to variables
  • Follow the sequential flow of variable assignments

3.2. Time Goal

  • 20 minutes on this section

3.3. Key Terms

assignment
A statement that gives a value to a variable.
initialize
To assign a variable for the first time.
state
The variables in a program and their current values.

3.4. Exercises

Note

After variables have been declared, we can use them to store values. We do that with an assignment statement.

message = "Hello!"; // give message the value "Hello!"
hour = 11; // assign the value 11 to hour
minute = 59; // set minute to 59

The above example shows three assignments, and the comments illustrate different ways people sometimes talk about assignment statements. The vocabulary can be confusing here, but the idea is straightforward:

  • When you declare a variable, you create a named storage location.
  • When you make an assignment to a variable, you update its value.

As a general rule, a variable has to have the same type as the value you assign to it. For example, you cannot store a “hello” in an integer or 4.5 in a String.

Note

For the problem below, you may find it helpful to enter each of the statements and run the program

    Q-57: Which of the following assignment statements are valid given the following declarations.

    int x;
    String value;
    
  • x = 5;
  • Correct
  • x = "5";
  • Incorrect
  • x = a;
  • Incorrect
  • value = 123;
  • Incorrect
  • value = "123";
  • Correct
        Q-59: Q-58: The following program segment should print Maria's first name on one line and her last name on the next line.

Maria
Hernandez
But the blocks have been mixed up and include an extra block that isn't needed in the solution. Drag the blocks from the left and put them in the correct order on the right. Click the Check Me button to check your solution.

String firstName; String lastName; --- firstName = "Maria"; lastName = "Hernandez"; --- System.out.println(firstName); --- System.out.println(lastName); --- System.out.print(firstname); #distractor --- string firstName; #distractor string lastName;

    Q-60: What is the value of x after all statements have executed

    int x;
    int i;
    
    x = 1;
    i = 10;
    x = i;
    
  • 1
  • Incorrect
  • 0
  • Incorrect
  • 10
  • Correct
  • "i"
  • Incorrect
  • #i
  • Incorrect
        Q-62: Q-61: In the following program, you are trying to swap the value ``4`` that is initially stored in ``i`` and the value``3`` stored in j.  At the end of your program, the code should print

i is 3
j is 4
The blocks have been mixed up and all code is required to complete the program. You may want to start by creating the program outline and adding comments. Drag the blocks from the left and put them in the correct order on the right. Also pay attention to the indentation. Click the Check Me button to check your solution.

public class Swap{ --- public static void main(String[] args){ --- // Declare variables --- int i; int j; int tmp; --- // Assign values to variables --- i = 4; j = 3; --- // Swap the variables in i and j --- tmp = i; --- i = j; --- j = tmp; --- // Output values --- System.out.println("i is " + i); --- System.out.println("j is " + j); --- } }
Next Section - Conditionals Lab