[an error occurred while processing this directive] CSE 142 Lab 2: Expressions, Variables, and Loops

University of Washington, CSE 142

Lab 2: Expressions, Variables, and Loops

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Basic lab instructions

Today's lab

Goals for today:

Expressions

Recall that Java has expressions to represent math and other computations. Expressions may use operators, which are evaluated according to rules of precedence. Every expression produces a value of a given type.

Type Description Expression Example Result
int integers (up to 231 - 1) 3 + 4 * 5 23
double real numbers (up to 10308) 3.0 / 2.0 + 4.1 5.6
String text characters "hi" + (1 + 1) + "u" "hi2u"
[an error occurred while processing this directive] [an error occurred while processing this directive]

jGRASP Interactions Pane

continued on the next slide...

[an error occurred while processing this directive]

Variables

Recall that you can use a variable to store the results of an expression in memory and use them later in the program.

type name;                       // declare
name = value or expression;        // assign a value
...
type name = value or expression;   // declare-and-initialize together

Examples:

double iPhonePrice;
iPhonePrice = 299.95;

int siblings = 3;
System.out.println("I have " + siblings + " brothers/sisters.");
[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]

for loops

A for loop repeats a group of statements a given number of times.

for (initialization; test; update) {
    statement(s) to repeat;
}

Example:

for (int i = 1; i <= 3; i++) {
    System.out.println("We're number one!");
}
System.out.println("/cheering");
Output:
We're number one!
We're number one!
We're number one!
/cheering

for loops

for (initialization; test; update) {
    statement(s) to repeat;
}
for (int i = 1; i <= 3; i++) {
    System.out.println("We're number one!");
}
System.out.println("/cheering");

The for loop keeps executing the println as long as the test condition is met:

  1. initialization :: int i = 1; :: start a counter at 1
  2. test :: i <= 3; :: continue as long as the counter i is less than 3
  3. execute the statements :: { System.out.println("We're number one!"); }
  4. update :: i++ :: add 1 to the counter
  5. go back to step 2 and check the test condition again: i is 1 bigger than it was last time through the loop

Once the test isn't true anymore, Java exits the for loop :: System.out.println("/cheering");

[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!