xxxxxxxxxx
boolean todayIsMonday = true;
boolean weAreHavingClass = true;
if (todayIsMonday && weAreHavingClass) {
System.out.println("Let's learn CS");
} else if (todayIsMonday) {
System.out.println("We celebrated MLK Day...");
}
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
xxxxxxxxxx
byte smallest = 10;
smallest += 256;
System.out.println(smallest);
> 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 May
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
xxxxxxxxxx
// Let's try out ==, !=, <, <=, >, and >=
int tester = 10;
// This is equality
System.out.println(tester == 10);
> 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
xxxxxxxxxx
int tester = 10;
// This is equality
System.out.println(tester == 10);
// This is not
System.out.println(tester = 5);
System.out.println(tester);
> Click or hit Control-Enter to run the code above
Variable Comparisons
xxxxxxxxxx
// Let's try out ==, !=, <, <=, >, and >=
int first = 10;
int second = 20;
System.out.println(first > first);
> Click or hit Control-Enter to run the code above
More Complex Comparisons
xxxxxxxxxx
// Let's try out ==, !=, <, <=, >, and >=
int first = 10;
int second = 20;
System.out.println(first + second == 30);
System.out.println(first - second <= 10);
> 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.
xxxxxxxxxx
int first = 10;
System.out.println(first > 5 && first < 10);
System.out.println(first < 10 && first > 5);
System.out.println(first > 10 || first > 5);
System.out.println(first > 10 || false);
> 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!
xxxxxxxxxx
int first = 10;
System.out.println((first > 0 && first < 10) || (first == 10));
System.out.println((first > 0 && first < 10) && (first == 10));
System.out.println(first < 10 && (first == 0 || first == 10));
> Click or hit Control-Enter to run the code above
Comparisons Enable Decisions
xxxxxxxxxx
// Comparisons enable decisions
int first = 10;
if (first > 20) {
System.out.println("First is greater than 20");
} else {
System.out.println("First is not greater than 20");
}
> 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.
xxxxxxxxxx
// Only the first matching branch of if is executed
int comparison = 10;
if (comparison > 5) {
System.out.println("I'll be executed");
} else if (comparison > 3) {
System.out.println("I won't be executed");
} else {
System.out.println("I definitely won't be executed");
}
> 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
xxxxxxxxxx
// Create an int called numSuperBowls, and then add 1 to it
// if sanFranciscoWinsAgain is true
> 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 }
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
xxxxxxxxxx
int outside = 5;
if (true) {
int inside = 10;
System.out.println(outside);
}
System.out.println(inside);
> 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.)
xxxxxxxxxx
int outside = 5;
if (true) {
int inside = 10;
// outside is to my left, so OK
System.out.println(outside);
}
// inside is to my right, so bad
System.out.println(inside);
> Click or hit Control-Enter to run the code above
Homework Progress
Please don’t get behind already!
-
HW0: 503
-
HW1: 481
-
HW2: 461
-
At this point you know enough to complete all of the available homework.
Preparing for Quizzes
Your first real CS 125 quiz starts today in the CBTF. Here’s how to prepare.
-
Don’t take the quiz on Monday before class. Quizzes may cover material introduced or reviewed on Monday.
-
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 3–5PM in Siebel 2227. Please come by and say hi!
-
Homework continues today.
-
Your first real quiz starts today in the CBTF and covers variables and conditionals—material up through now.
-
We have office hours from 12–4PM today in Siebel 0403.
-
Please fill out the initial student survey. 1% extra credit for anyone who does by Sunday 2/2/2020.