eecs 183 - university of michigandrgage/eecs183/3/week_3.pdf · eecs 183 week 3 - diana gage ...

33
EECS 183 Week 3 - Diana Gage www-personal.umich.edu/ ~drgage

Upload: others

Post on 22-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

EECS 183Week 3 - Diana Gage

www-personal.umich.edu/~drgage

Page 2: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Upcoming Deadlines� Lab 3 and Assignment 3 due October 2nd (this

Friday)� Project 2 will be due October 6th (a week from

Friday)�  Get started early!

Page 3: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

New Material: Conditionals� What is a conditional?

�  A statement with a condition

�  If something evaluates to true, we want to do one thing

�  If that thing evaluates to false, we want to do something else

^ general idea

Page 4: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

New Operations and Operators&& ‘and’|| ‘or’! ‘not’ (negation)

= assignment operator== comparison operator

Page 5: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

< less than> greater than<= less than or equal to>= greater than or equal to!= not equal to

New Operations and Operators

Page 6: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Example: if statement

int age = 23;

if (age >= 18) {cout << “You can get a

tattoo!”;cout << endl;

}

Page 7: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

&& -- The ‘and’ operator� Both pieces of the conditional must evaluate to

true

� Sample code:if (sunny && above_60) {

cout << “Let’s throw a disc outside!”; cout << endl;

}� Both must be true

Page 8: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

I I – The ‘or’ operator�  One or more pieces of the conditional must evaluate to

true�  Sample code:

if (sunny || above_60) {cout << “I guess we can throw a disc outside”;cout << endl;

}�  One, or the other, or both, must be true

Page 9: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

! – The ‘not’ operator� This is the negation operator�  It takes the truth value of the variable or

expression in question, and negates it

bool has_green_hair = true;cout << !has_green_hair << endl;

What does this print?

Page 10: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

! – The ‘not’ operator� This is the negation operator�  It takes the truth value of the variable or

expression in question, and negates it

bool has_green_hair = true;cout << !has_green_hair << endl;

What does this print? 0

Page 11: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

if, else if, else statements� Often conditionals are a group of

statements comprised of:�  if (…) {…}�  else if (…) {…} (there can be many else ifs)�  else {…}

� These are called branches�  Notice how the else statement does not have

parentheses�  “Catch all” à doesn’t need condition

Page 12: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Branching� Statements are dependent on each

other in the order the code is written� The else if follows directly from the if,

and the else follows from both the if and else if

Page 13: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Branching�  if (…) {…}

� This always goes first, and it will either execute (if true) or not (if false)

�  else if (…) {…})� This runs only if the previous if statement did not

execute�  else {…}

� This runs only if neither of the previous statements executes

� This will ALWAYS execute if neither of the previous statements did

Page 14: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Example of Branching:if (sunny && above60) {

cout << “Let’s play outside!” << endl;}else if (rainy) {

cout << “Let’s watch a movie!”;cout << endl;

}else {

cout << “I can’t decide!” << endl;}

Page 15: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Branching� There can be as many else ifs as you want� But only one if and one else per branch� This doesn’t mean you can’t have many if

statement in a row�  Each will always execute, regardless of the

previous one�  Sometimes this is what you want!

Page 16: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

= – The Assignment Operator

� This operator assigns whatever is on the left to whatever is on the right

int k = 25;

Page 17: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

== – The Comparison Operator� This operator compares whatever is on the

left to whatever is on the right� This comparison will evaluate to true or false� Either the two sides are the same, or not� Be careful with the difference between =

and ==

Page 18: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

IMPORTANT difference: = vs. ==

� Be careful not to use the assignment operator within a conditional

�  Instead of checking whether they are the same (true) or different (false)…�  The line of code will set the left equal to

whatever was on the right�  This is probably not what you want

Page 19: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Common error: = vs. ==// Let’s say the following code is in a functionint a = 30;int b = 25;

if (a == b) {cout << “equal” << endl;return true;

}

if (a = b) {cout << “equal” << endl;return true;

}

Page 20: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Common error: = vs. ==// Let’s say the following code is in a functionint a = 30;int b = 25;

if (a == b) {cout << “equal” << endl;return true;

}

if (a = b) {cout << “equal” << endl;return true;

}

The if statement won’t execute because a is not equal to b

Page 21: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Common error: = vs. ==// Let’s say the following code is in a functionint a = 30;int b = 25;

if (a == b) {cout << “equal” << endl;return true;

}

if (a = b) {cout << “equal” << endl;return true;

}

Page 22: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Common error: = vs. ==// Let’s say the following code is in a functionint a = 30;int b = 25;

if (a == b) {cout << “equal” << endl;return true;

}

if (a = b) {cout << “equal” << endl;return true;

}

-  a will be set to 25-  the value 25 is true-  the statements

inside will execute-  but that’s incorrect!

Page 23: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Important Difference: � x < y < z vs. x < y && y < z� Reminder:

�  Anything but 0 à true�  0 à false

� You want to use x < y && y < z because…

Page 24: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage
Page 25: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Short-circuit evaluationbool print_hello() { cout << “Hello” << endl; return true;}

int main() { bool sunny = true; if (sunny && print_hello()) { cout << “It’s sunny and we said hello!”; } else if (sunny || print_hello()) {

cout << “it’s sunny, or we said hello, or both!”; } else { cout << “It’s not sunny and we didn’t say hello.”;}What happens? What prints?

Page 26: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Short-circuit evaluationbool print_hello() { cout << “Hello” << endl; return true;}

int main() { bool sunny = true; if (sunny && print_hello()) { cout << “It’s sunny and we said hello!”; } else if (sunny || print_hello()) {

cout << “it’s sunny, or we said hello, or both!”; } else { cout << “It’s not sunny and we didn’t say hello.”;}Because of short-circuit evaluation, if the first element in an or statement conditional evaluates to true, the rest of the conditional won’t even be checked

Page 27: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Style with Conditionals�  Do not do

if (true) or if (a == false)

�  Do not compare doubles�  Do not compare things of different types

�  i.e. string and doubles�  Use consistent brackets�  Don’t over-complicate expressions

if (!(a == b)) vs. if (a != b)

Page 28: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Quick Note: RME “Requires” Clause�  You are the programmer. You write the function.�  A client calls this function. �  The programmer should write the function assuming

provided input will follow the Requires clause.�  Client’s job to read the RME of any function they use.

�  If they violate the requires clause, the program will still run, but may have undefined behavior.

�  Example:// Requires: denominator is not 0double divide (int numerator, int denominator);

Page 29: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Lab� Two parts:

�  Exam Practice� Answer the question in lab3.pdf in 10 minutes (I will time

you)� Write your answer with pencil and paper

�  Testing functions� This will be very important for Project 2 and beyond!

Page 30: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Exam Practice� Write your answers to the question just like you will

on the exam� Take at most ten minutes to solve the problem.

Remember you will only have 90 minutes for the upcoming exam!

� After the 10 minutes, we will show the answer and discuss the solution

� You do not need to submit the answer, but keep it to help you review!

Page 31: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Exam Practice Solution

Page 32: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Testing with functions� Now complete the Testing section of the lab

assignment� Feel free to ask questions� You need to submit this portion of the lab to

the autograder to receive 5 points

Page 33: EECS 183 - University of Michigandrgage/eecs183/3/Week_3.pdf · EECS 183 Week 3 - Diana Gage  ~drgage

Feel free to ask any questions after class!