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 many common programming errors
Our Types
In CS 125 we’ll primarily be dealing with just a few of Java’s primitive types:
-
int
for storing integers -
double
for storing floating point values -
boolean
for storing truth values
Questions About Variables or Types?
I Want You Here Until December
All of you. Especially the beginners.
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 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
Variable Comparisons
> 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 and Grouping
There are a few more rules that Java applies when evaluating conditionals expressions:
-
Conditionals are always evaluated from left to right, and evaluation stops as soon as the result is known.
-
Comparisons can be grouped using parentheses (
(
and)
) -
But we’ll try and keep our comparisons simple for now—and you should too!
> Click or hit Control-Enter to run the code above
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: At Most One Statement Runs
In any if statement, at most one statement is executed.
-
If there is an
else
statement exactly one statement runs -
If there is not an
else
statement at most one statement runs, but it’s possible than no statement runs
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
Homework Progress
Please don’t get behind already!
-
HW0: 862
-
HW1: 844
-
HW2: 803
-
HW3: 675
Preparing for Quizzes
Your first real CS 125 quiz starts next week in the CBTF. Here’s how to prepare.
-
Multiple-choice questions will test content covered in class—so review the slides
-
Also read the assigned chapter in "Coders".
-
Programming questions will be quite similar to the homework problems—so review them too
-
You have unlimited attempts on the programming questions but limited attempts on the multiple-choice questions
Announcements
-
I have office hours today from 1–3PM in Siebel 2227. Please come by and say hi!
-
Homework continues today, over the weekend, and on Monday.
-
No class or office hours on Monday (Labor Day) 1.
-
Your first real quiz will start next week in the CBTF and cover variables and conditionals—material up through today.
-
We have office hours until 5PM today.
-
Please fill out the initial student survey. 1% extra credit for anyone who does by Sunday 09/08/2019.