Comparisons and conditional expressions
> Click or hit Control-Enter to run the code above
What Are Computers Good At?
-
Basic math
-
Simple decision making
-
Doing things over and over again very, very fast
-
And storing data
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
Next Time: Algorithms and Loops
Announcements
-
The first set of Turing’s Craft exercises are due tomorrow by midnight.
-
CS 199 EMP and CS 196 (the honors section) open for registration today at noon.
-
MP0 is out and due Friday! At this point you should be able to finish
Quizzer
. Office hours until 7PM today. -
Please fill out the intro survey! 1% extra credit for anyone who does by today at noon.