Top Banner
CS 121 Week 4 - Monday
41

Week 4 - Monday. What did we talk about last time? Wrapper classes if statements.

Jan 02, 2016

Download

Documents

Toby Wade
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

CS 121Week 4 - Monday

Page 2: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Last time

What did we talk about last time? Wrapper classesif statements

Page 3: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Questions?

Page 4: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Project 1

Page 5: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Conditional Execution Review

Page 6: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Conditional execution

So far we have only considered Java programs that do one thing after another, in sequence

Our programs have not had the ability to choose between different possibilities

Now, they will!

Page 7: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Behold!

The if-statement:

x is small will only print out if x is less than 5

In this case, we know that it is, but x could come from user input or a file or elsewhere

int x = 4;

if( x < 5 )System.out.println("x is small!");

Page 8: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

The if part

Any boolean expression

Any single executable statement

Anatomy of an if

if( condition ) statement;

Page 9: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Conditions

Page 10: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Conditions in the if

Any statement that evaluates to a boolean is legal

Examples: x == y true Character.isDigit('r') s.equals("Help me!") && (z < 4)

Page 11: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Comparison

The most common condition you will find is a comparison between two things

In Java, that comparison can be: == equals != does not equal < less than <= less than or equal to > greater than >= greater than or equal to

These are called relational operators

Page 12: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Equals

You can use the == operator to compare any two things of the same type

Different numerical types can be compared as well (3 == 3.0)

Be careful with double types, 0.33333333 is not equal to 0.33333332

int x = 3;if( x == 4 )System.out.println("This doesn't print");

Page 13: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Not Equals

Any place you could have used the == operator, you can use the != operator

If == gives true, the != operator will always give false, and vice versa

If you want to negate a condition, you can always use the ! as a not

is the same as

if( x != 4 )

if( !(x == 4) )

Page 14: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

= != ==

Remember, a single equal sign (=) is the assignment operator (think of a left-pointing arrow)

A double equals (==) is a comparison operator

int y = 10;if( y = 6 ) //compiler error!

boolean b = false;if( b = false ) //no error but wrong

Page 15: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Less Than (or Equal To)

Inequality is very important in programming

You may want to take an action as long as a value is below a certain threshold

For example, you might want to keep bidding at an auction until the price is greater than what you can afford

Watch for strict inequality (<) vs. non-strict inequality (<=)

if( x <= 4 )System.out.println("x is less than 5");

Page 16: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Greater Than (or Equal To)

Just like less than or equal to, except the opposite

Note that (because of the All-Powerful Math Gods) the opposite of <= is > and the opposite of >= is <

Thus, !( x <= y ) is equivalent to ( x > y ) !( x >= y ) is equivalent to ( x < y )

Page 17: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Else

Page 18: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Either/Or

Sometimes you have to make a decision

If a condition is true, you go one way, if not, you go the other

For example: If I pass CS121,▪ Then I throw a kegger to celebrate

Otherwise,▪ I punch Dr. Wittman in the face

Page 19: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Exclusivity

Notice the nature of this kind of condition

Both outcomes cannot happen Either a kegger gets thrown or Dr.

Wittman gets punched in the face For these situations, we use the else

construct

Page 20: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Anatomy of an if-else

Two different

outcomes

if( condition ) statement1;

elsestatement2;

Page 21: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

else example

Scanner in = new Scanner(System.in);int balance = in.nextInt();

if( balance < 0 )System.out.println("You are in debt!");

elseSystem.out.println("You have a $" + balance);

Page 22: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Multiple Statements and Nesting

Page 23: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

What if you need to do several things conditionally?

No problem Use braces to treat a group of

statements like a single statement

if( x == 4 ){System.out.println("I hate 4");System.out.println("Let's change x.");x = 10;

}

Page 24: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

An if with multiple statements

if( condition ){

statement1;statement2;…statementn;

}

A whole bunch of

statements

Page 25: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Nesting

Sometimes you want to make one set of decisions based on another set of decisions

if-statements can be nested inside the bodies of other if-statements

You can put if-statements inside of if-statements inside of if-statements… going arbitrarily deep

Page 26: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Nested ifs

if( condition1 ){

statement1;if( condition2 ) {

if( condition3 )statement2;

…}

}

Page 27: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

An example using quadrants

For the next example, recall the 4 quadrants of the Cartesian coordinate system

x-x

y

-y

(0,0)

12

3 4

Page 28: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Nesting example

Find which quadrant the point (x,y) is inif( x >= 0.0 )

{if( y >= 0.0 )

System.out.println("Quadrant 1");else

System.out.println("Quadrant 4");}else{if( y >= 0.0 )

System.out.println("Quadrant 2");else

System.out.println("Quadrant 3");}

Page 29: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

if and else if

You can list a sequence of exclusive possibilities using nesting:

if( index == 1 ) System.out.println("First");

else if( index == 2 )System.out.println("Second");

else if( index == 3 )System.out.println("Third");

elseSystem.out.println(index + "th");

Page 30: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

else-if doesn’t actually exist

A block of code is treated just like one statement

A whole if-else is treated the sameif( … )

statement1;else if( … )

statement2;else

statement3;

if( … ){

statement1;}else{if( … )

statement2;else

statement3;}

=

Page 31: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Pitfalls

Page 32: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Watch out!

Now you are controlling the flow of execution in your program

There is a wider range of mistakes you can make when giving instructions

Huge chunks of code can be executed or skipped by mistake

Here are a few things to watch out for

Page 33: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Empty statements

Remember that an if-statement is not an executable statement

It does not end with a semicolon

if( balance < 0 ); // empty statement{ // this block always runsSystem.out.println("You owe a fee!");balance -= 15;

}

Page 34: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Confusing indentation

In some languages, indentation actually matters

Java ignores whitespace

"Fight!" prints no matter what

if( enemies > 2 ) System.out.println("Run away!");

elsedefense = true;System.out.println("Fight!");

Page 35: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Imprecise conditions

It’s easy to make logical errors when writing conditions

If an airline allows two or fewer bags on the plane, someone might code that as:

But this is too restrictive. It should be:

if( bags < 2 ) // only allows 1 or 0boarding = true;

if( bags <= 2 )boarding = true;

Page 36: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Reversed conditions

Sometimes it’s easy to get a condition backwards

Try not to assume you wrote the condition correctly

Always double check

if( number % 3 == 0 ) System.out.println("Not divisible by 3!");

elseSystem.out.println("Divisible by 3!");

Page 37: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

if Examples

Page 38: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Speed limit

Sometimes you probably break the speed limit

But, there's one speed limit you can never break

The speed of light c is about 3 x 108 m/s

Given a variable named speed of type double, what's an if-statement that will print an error message if speed is larger than c?

Page 39: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Upcoming

Page 40: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Next time…

Examplesswitch statements

Page 41: Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.

Reminders

Keep reading Chapter 4 of the textbook

Keep working on Project 1 (due this Friday)

Exam 1 is next Monday