Comparisons and conditional expressions
> Click or hit Control-Enter to run the code above
Review: Variable Types
Java has eight primitive data types.
All other data in Java is represented by combinations of these building blocks. You can break them into four categories:
-
Integers:
byte
,short
,int
,long
-
Floating point numbers:
float
,double
-
Character:
char
-
True or false:
boolean
Why Are There Multiple Numeric Types?
-
Integers:
byte
,short
,int
,long
-
Floating point numbers:
float
,double
Different types take up different amounts of computer memory and so can store different values.
Don’t worry too much about how things are stored yet. But the limits are important to understand.
Type Limitations
> Click or hit Control-Enter to run the code above
Why Types
-
Types force you to specify how much space you need to store your data. That can make your program more efficient
-
Types also help catch some common programming errors
What Are Computers Good At?
-
Basic math
-
Simple decision making
-
Doing things over and over again very, very fast
-
Storing data
-
And communicating
Comparisons
Java also allows me to compare different variables: either against literal values, or against other variables.
This is basis for simple decision making.
Simple Comparisons
> Click or hit Control-Enter to run the code above
A Common Mistake
-
a = 10
means set a to 10 -
a == 10
means test if a is equal to 10
Don’t Get Confused
> Click or hit Control-Enter to run the code above
Comparisons Against Variables
> Click or hit Control-Enter to run the code above
More Complex Comparisons
> Click or hit Control-Enter to run the code above
Questions About Simple Comparisons?
Compound Comparisons
We can combine multiple comparisons together using logical and (&&
) and or
(||
) operators.
> Click or hit Control-Enter to run the code above
Evaluation Order
Conditionals are always evaluated from left to right, and evaluation stops as soon as the result is known.
Comparisons Enable Decisions
> Click or hit Control-Enter to run the code above
if-else
if-else
statements are the building block for programmatic decision making:
-
if something is true, do one thing;
-
otherwise, if something else is true, do some other thing;
-
otherwise, if something else is true, do some other thing;
-
etc.
if-elseif-elseif-else
if
statements can have multiple clauses:
if (firstThing) {
// do one thing
} else if (secondThing) {
// do another thing
} else if (thirdThing) {
// another thing
} else {
// run if firstThing, secondThing, and thirdThing are all false
}
else
statements are run if no if statement matches.
> Click or hit Control-Enter to run the code above
Remember: Only One Statement Runs
In any if statement, only one statement is executed.
Somewhat Useful Example
> Click or hit Control-Enter to run the code above
Nested Conditionals
if
statements can be nested inside other if
statements
if (testMe) {
if (testMeAgain) {
// I am well tested
} else {
// I'm only moderated well-tested
}
} else {
// You need to write better tests
}
> Click or hit Control-Enter to run the code above
Variable Scope
if
statements provide our first example of a block of code.
if (...) {
// I'm a block of code
}
-
Blocks are enclosed by braces and can have multiple statements
-
Variables declared inside a block are not visible outside it…
-
But variables declared outside (and prior to) a block are visible inside it
> Click or hit Control-Enter to run the code above
Indentation Hints
Assuming you are indenting your code properly, a general rule of thumb about scope:
-
You can access variables "to the left"
-
You cannot access variables "to the right"
(Where directions are defined relative to the start of the line of code you are writing.)
> Click or hit Control-Enter to run the code above
Monday: No Course Activities (Labor Day)
Wednesday: Algorithms and Loops
Friday: This Guy Ruins Class

Homework Progress
Please don’t get behind already!
-
HW0: 890
-
HW1: 867
-
HW2: 844
-
HW3: 702
Announcements
-
No class Monday (labor day)…
-
But we will release MP0 on Monday!
-
We have office hours until 5PM today.
-
But no office hours this weekend or Monday.
-
Please fill out the initial student survey. 1% extra credit for anyone who does by Sunday 09/09/2018.