CS110_03b_if - EECS110 3b Decision-Making Statements Jack...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 3b Decision-Making Statements Jack Tumblin [email protected] Review: relational and logical operators; precedence. If/else statements; orderly, nested decisions `switch' or `case' statements. Review: Relational Operators Operator < > <= >= Action Less than Greater than Less than or equal Greater than or equal Is Equal Is Not Equal == != Result is always either TRUE or FALSE Review: Logical Operators Different from Relational Operators because: Inputs: TRUE or FALSE Outputs: TRUE or FALSE Operator && || ! Action AND OR NOT Review: Operator Precedence: ! Arithmetic, Relational, Logical, Assignment Unary - * / % + < <= >= > = = != && || = Higher precedence (do these first) Associativity: execute left-to-right (except for = and unary (see book)) Example: 5+2-4+3 7-4+3 3+3 6 Making Decisions Sometimes your programs will need to make logical choices. (how? use logical, relational operators) Example: IF the light is green THEN cross the street ELSE wait for the light to change Making Decisions in C One statement, 3 parts: condition if (light_color == green) { cross_the_street(); } else { wait_for_light_to_change(); }; Making Decisions in C One statement, 3 parts: condition if (light_color == green) { cross_the_street(); } TRUE PART : one (block of) statement(s) else { wait_for_light_to_change(); }; Making Decisions in C One statement, 3 parts: condition if (light_color == green) { cross_the_street(); } TRUE PART : one (block of) statement(s) else { wait_for_light_to_change(); }; FALSE PART : one (block of) statement(s) Making Decisions in C: if() Sometimes there is no `else', there is no FALSE part: Example: if(gas_tank_empty==TRUE) { fill_up_tank(); }; If the condition is false (e.g. if the gas tank is not empty), then do nothing. Decision-Making Statements if(condition)statement; if (condition) { statements; statements; ... } if (condition) { statements; statements;... } else { statements; statements;... }; if statement has 3 Forms (note indenting) Note condition is always in parentheses, and All TRUE parts and all FALSE parts are a single statement, or a single block of statements {} Nested Decisions "Orderly Nesting: it's a Good Thing." -Martha Stewart A very Big Idea in CS Simplify, organize any complicated logic by using nested decision statements if(isHungry(Lisa) && isHungry(Ed)) { if(isHere(Boss) && isHungry(Boss)) { eat(FOOD_FANCY); shiftAppts(12.0,pm,2.0,pm); } else { if(isHere(Jim) && isHere(Lori)) { eat(FOOD_CAFETERIA); } else { if(getTime() > MID_DAY) { eat(FOOD_BROWNBAG); } else cancelLunch(); } } } Formatting for Nested Decisions Sometimes you will need more than two parts. Use nested if/else statements (can get very messy--careful indenting helps) if(people < 5) { drive_car(people); } else { if(people < 15) { drive_van(people); } else { if (people < 50) { drive_bus(people); } else { cancel_event(people); } } } Or . . . Formatting for Nested Decisions Sometimes you will need more than two parts. Use nested if/else statements (can get very messy--careful indenting helps) if(people < 5) { drive_car(people); } else { if(people < 15) { drive_van(people); } else { if (people < 50) { drive_bus(people); } else { cancel_event(people); } } } Or . . . Notice the pattern.... Nested Decisions: else if Sometimes you will need more than two parts. Use nested if/else statements (can get very messy--careful indenting helps) if(people < 5) { drive_car(people); } else if(people < 15) { drive_van(people); } else if (people < 50) { drive_bus(people); } else { cancel_event(people); } Skip nesting: use else if(cond) {...} instead of else { if(cond) {...} } Many Decisions: switch() switch(arg) { case 0: statement; statement; break; case 2: statement; break; case 3: statement; break; default: statement; break; } Too many nested if or elseif statements? Confusing to read, Slow to execute. Use switch() instead. (CS Jargon: `switch' or `case statements') arg=0; Many Decisions: switch() A sTrAnGe OnE! Input arg value selects a case val: label, (colon, NOT semi-colon!) switch(arg) { case 0: statement; statement; break; case 2: statement; break; case 3: statement; break; default: statement; break; } begins execution there, stops at break; arg=3; Many Decisions: switch() Start at case val: and Stop at break; Efficient! Jumps directly to the proper case val: label switch(arg) { case 0: statement; statement; break; case 2: statement; break; case 3: statement; break; default: statement; break; } arg=0; Many Decisions: switch() Start at case val: and Stop at break; No break; found? Keep going ... switch(arg) { case 0: statement; statement; statement; case 2: statement; break; case 3: statement; break; default: statement; break; } arg=1; Many Decisions: switch() No matching case val: found? Use default: statement(s) DETAILS: Input arg is a variable, but Value val in the case val: labels MUST be a literal; no variables allowed! switch(arg) { case 0: statement; statement; break; case 3: statement; break; default: statement; break; } Cryptic: Conditional Expressions testExp ? trueExp : falseExp a==b?c--:c++; /*AWFUL!*/ /*MUCH BETTER:-------if(a==b) // same value? { c--; /*YES! one less mismatch now.*/ } else // NO. { c++; /* No. One more mismatch to fix*/ } Quaint, useless, confusing, no room for comments, Know how to read it, but never use it in your C code. A truly BAD IDEA. Never confuse `compact' notation for `efficient' or `sophisticated'. No longer makes code faster; it's just less readable and more error-prone. Cryptic: Conditional Expressions if ( condition ) var=expression1; else var=expression2; var=condition ? expression1 : expression2 ; BAD IDEA! if ( condition ) statement1; else statement2; Examples: max = (x>y) ? x : y ; (x>0) ? printf("positive") : printf("negative"); condition ? statement1 : statement2 ; CAREFUL! MANY Next Class: Loops (easy) PAGES TO READ! Loop statements repeat a block (of other statements) for as many times as you wish. A terminating condition tells a loop when and how to stop repeating those statements No terminating condition? Your program will never get finished! 1 of 3 ways to repeat a block of statements for(init; cond; step){stmts;} init cond ){ // Frog lifetime int days; //for( init; cond; step) for(days=155; days>0; days--) { work_all_day(); sleep_all_night(); } die_quietly(); start no is(cond)true? yes { block of statements; } Preview: For loops in C: finish ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online