CSE 142 Chapter 4 Programming I l Read Sections 4.1–4.5, 4.7–4.9 l The book assumes that you’ve read chapter 3 on functions ➤ Read it anyway, you should do fine Conditionals ➤ Their order is a little screwy... 28. June, 2000 F-2 CSE 142 Summer 2000 — Isaac Kunen 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-1 Preview of Things to Come Preview Prologue l Control flow is the order in which l We’ll look at ways to change the flow: statements are executed l Until now, control flow has been sequential: Conditionals Functions Loops 28. June, 2000 F-3 28. June, 2000 F-4 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Conditionals Conditional (“if”) Statement if (condition) statement; l Conditionals let the computer choose an execution path depending on the value of an expression l The statement is executed only if the ➤ Print an error if the withdrawal amount is condition is true. more then what is in the account ➤ Add one to my age if it is my birthday if (age >= 21) ➤ If my grade is greater than 3.5, then celebrate printf(“Have a beer!\n”); 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-5 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-6 1
Conditional Flow Chart Conditional Expressions l Also called “logical”, or “Boolean” if (x < 100) x = x + 1; expressions y = y + 1; l Make use of relational operators yes l A relational operator compares two values X < 100 ? x = x + 1 l Examples: (x < 30) no (12 > y) y = y + 1; 28. June, 2000 F-7 28. June, 2000 F-8 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Relational Operators Some Conditional Expressions In Math In C In English air_temperature > 80 < < Less Than > > Greater Than 98.6 == body_temperature = == Equal To <= Less Than or Equal To ≤ marital status == ‘M’ >= Greater Than or Equal To ≥ != Not Equal ≠ weight >= 8000 28. June, 2000 F-9 28. June, 2000 F-10 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Multiple Actions Example: Checking for Error l What if we want to do more things at once? printf(“Enter divisor: ”); ➤ Use a compound statement! scanf(“%lf”, &divisor); l Replace the statement with several if (0 == divisor){ statements surrounded by braces: { } printf(“Can’t divide by 0!\n”); ➤ Sometimes called a block exit(0); ➤ Indent each block! } No ; 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-11 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-12 2
Announcement! Value of Boolean Expressions l Now you know enough to do homework 1 l Remember, expressions are things in C that have values l What’s the value of a conditional Yay! expression? 28. June, 2000 F-13 28. June, 2000 F-14 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Values of Boolean Expressions Examples: l Conditional expressions are either true or l Do these examples make sense? false l If so, what are the values assigned? l C doesn’t have a Boolean Type l C fakes it using integers! foo = 7 < 0; ➤ 0 means false ➤ non-zero (usually 1) means true bar = 8 != 3; 28. June, 2000 F-15 28. June, 2000 F-16 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen More Strangeness! The Value of an Assignment l In fact, this is an expression : l What does this do? x = 6 + 7 int foo, bar; l If it is an expression, then it has a value foo = (bar = 6); l The value of an assignment is the value assigned 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-17 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-18 3
Why Am I Telling You This?! Strange Example I l It’s easy to confuse = and == l What does this do? l If you confuse them, the program will compile and run, but it won’t do what you if (x = 7) want it to printf(“X is equal to 7\n”); l Watch out for this error! 28. June, 2000 F-19 28. June, 2000 F-20 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Strange Example II Lesson: l What does this do? l C is very picky ➤ Getting one character wrong will often make your program work incorrectly! x == PI * radius * radius; l You’ll have to get very good at finding these stupid little errors ➤ Practice practice practice! 28. June, 2000 F-21 28. June, 2000 F-22 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen One More Strange Point Complex Conditionals l We’d like more expressive power l Do not use == or != with doubles ➤ If I have at least $15 or you have at least $15, ➤ doubles may not have exact values, only then we can go to the movies. approximations ➤ If you’re in Guggenheim 224 and it’s 12:00 on ➤ == and != are only make sense if the values Friday, then you’re in CSE 142. are exact ➤ If you’re in CSE 142 and your name is Isaac, then you’re the lecturer. ➤ < , > , etc. are fine for use with doubles 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-23 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-24 4
Boolean Operators Complex Conditionals in C l Boolean operators act on Boolean values if ((myMoney > 15.0) || to produce new Boolean values (yourMoney > 15.0)){…} C English if ((location == 224) && && And (time == 12 )){…} || Or (sort-of…) ! Not if (!(initial == ‘I’)){…} 28. June, 2000 F-25 28. June, 2000 F-26 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Truth Tables for && , || Or isn’t Always Like in English! l A truth table lists all possible combinations l In English if “this fruit is an apple or it is an of values and their result orange”, then it cannot be both apple and orange P Q P && Q P || Q l In C, T T T T T F F T (fruit == ‘A’) || (fruit == ‘O’) F T F T F F F F is true if either half is true! 28. June, 2000 F-27 28. June, 2000 F-28 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Truth Table for ! An Example Expression l && and || are binary operators l foo is an integer that is not 5, 6, or between 10 and 20 l ! is a unary operator that inverts the value !((foo == 5) || (foo == 6) || P !P ((foo > 10) && (foo < 20))) T F F T 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-29 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-30 5
DeMorgans’ Laws DeMorgan’s Laws l Convert between and expressions and or l DeMorgan’s Laws tell us some legal expressions conversions: l Example: !(P && Q) (!P || !Q) !((age < 25) && (sex == ‘M’)) is equivalent to !(P || Q) (!P && !Q) ((age >= 25) || (sex != ‘M’)) 28. June, 2000 F-31 28. June, 2000 F-32 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen Proof of DeMorgan’s Laws Else: The Other Half of If l else lets you do something if the condition l Proof using a truth table: was false P Q (P && Q) !(P && Q) !P !Q (!P || !Q) if (balance >= withdrawal){ balance = balance - withdrawal; T T } T F else { F T printf (“Insufficient Funds!\n”); F F } 28. June, 2000 F-33 28. June, 2000 F-34 CSE 142 Summer 2000 — Isaac Kunen CSE 142 Summer 2000 — Isaac Kunen if-else Control Flow Nested ifs #define BILL_SIZE 20 if (balance < withdrawal) { no yes printf(“Insufficient funds!\n”); balance >= withdraw ? } else { if (withdrawal < BILL_SIZE) adjust balance printf (“Try a larger amount. \n”); Insufficient Funds! dispense funds else balance = balance - withdrawal; } arrive here whether condition is TRUE or FALSE 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-35 28. June, 2000 CSE 142 Summer 2000 — Isaac Kunen F-36 6
Recommend
More recommend