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
-
Mouse over highlighted words if you're not sure what they mean!
-
Talk to your classmates for
help.
-
You may want to bring your textbook to future labs
to look up syntax and examples.
-
Stuck? Confused? Have a question? Ask a TA for
help, or look at the book or past .
-
Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave.
-
Feel free to complete problems in any order.
-
Make sure you've signed in on the sign-in sheet before you leave!
Today's lab
Goals for today:
-
write and evaluate expressions to compute numeric
values
-
use variables to store results of a computation into
memory
-
trace and write
for
loops for repeating
lines of code
-
draw patterned figures of text using loops
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
-
jGRASP has a useful feature called the Interactions Pane that allows you to type in Java expressions or statements one at a time and instantly see their results.
-
To use it, run jGRASP and then click the "Interactions" tab near the bottom.
-
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;
name = value or expression;
...
type name = value or expression;
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:
- initialization ::
int i = 1;
:: start a counter at 1
- test ::
i <= 3;
:: continue as long as the counter i
is less than 3
- execute the statements ::
{ System.out.println("We're number one!"); }
- update ::
i++
:: add 1 to the counter
- 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!