210 likes | 323 Views
Conditional Execution & Branching. Instructor Nash Readings: “Cat” Book, Section 3.1. Robert Frost – Mountain interval. Written in 1920. Robert Frost - “The Road Less Taken”.
E N D
Conditional Execution & Branching • Instructor Nash • Readings: “Cat” Book, Section 3.1
Robert Frost – Mountain interval • Written in 1920
Robert Frost - “The Road Less Taken” • Two roads diverged in a yellow wood,And sorry I could not travel bothAnd be one traveler, long I stoodAnd looked down one as far as I couldTo where it bent in the undergrowth.… • I shall be telling this with a sighSomewhere ages and ages hence:Two roads diverged in a wood, and I--I took the one less traveled by,And that has made all the difference.
The Basic IF If (< booleanCondition>) { <statement> … <statement> } • Called a Selection Control Structure • The statements are said to be “controlled” by the if • Notice if the condition is false, nothing is done!
The IF/ELSE (Singular) If (< booleanCondition>) { <statement> … <statement> } else { //this is a catch-all <statement> … <statement> } • Notice that one of the two blocks of code WILL be executed • Also, the two blocks are Mutually Exclusive
A Nested IF/ELSE Chain (no default) • Also called nested “If” statements • Two types: with or without a trailing “catch-all” else • This type doesn’t guarantee any block will be executed • But if one is, only one block is executed, then we exit the structure If (< booleanCondition1>) { <block> } else if (<booleanCondition2> { //!BC1 <block> } else if (<booleanCondition3> { //!BC1 && !BC2 <block> } else if (<booleanCondition4> { //!BC1 && !BC2 && !BC3 <block> }
A Nested IF/ELSE Chain With Default • Last “else” is like a “catch all” • This type forgoes the last if condition test • So, if all of the previous conditions fail, you will always do the last block of code • This guarantees one and only one block will be executed • If all condition tests fail, then the last else is the default block to be executed If (< booleanCondition1>) { <block> } else if (<booleanCondition2> { //!BC1 <block> } else if (<booleanCondition3> { //!BC1 && !BC2 <block> } else { //!BC1 && !BC2 && !BC3 <block> }
Nested IF/ELSE Statements • The blocks are Mutually Exclusive • Either 1 or none of the “controlled” blocks will be executed • If we have a tail “catch-all”, then we know one and only one will be executed • Pick your Control Structure depending on the task at hand • Do you need one chunk of code to always execute • OR, is it ok to not execute the body of any of the if statements?
Basic IF Statements In Sequence if( condition1) { //notice these ifs are NOT related } if(condition2) { //if both conditions are true, both bodies will be //executed! Not mutually exclusive } //Contrast single if statements in sequence to //a chain of nested if statements • The chained ifs are related to one another: M.E. • The sequence above are unrelated, and all blocks might execute • See ConditionalExecution.docx
To Relate Two IFs or Not To Relate • What’s the difference between 3 single (sequential) if statements VS 3 IFs chained together? Consider efficiency: • The basic sequential ifs will each execute their test no matter what (3x) • The chained IF/ELSE structure will only execute tests until one passes (and skips the rest) (1x-3x)
A Tail Else “CatchAll” V.S. A Tail Test • A nested IF/ELSE structure ending in: • } else { //one less test here! • //If you made it to the else above, you’ll always dive in • } • Versus a nested IF/ELSE structure ending in: • } else if ( lastTest ) { • //just making it to the test above doesn’t guarantee • //this block will execute – depends on the last test • }
Sequential IFs and Nested IF/ELSEs • If you want to execute any combination of code blocks (controlled statements) • Use sequential single if statements • If you want to execute one or none of the code blocks • Use nested IF/ELSEs ending in a test • If you want to execute exactly one code block • Use nested IF/ELSEs ending in an else
Boolean Conditions • These are expressions that evaluate to a TRUE or FALSE value • We use these for loop tests as well as for IF tests • To construct an expression that yields a T/F value, use a relational operator! • X < 5 • A > B & A > C • A != D • F == G • Primitives only here; object equality later on
Table 4.1 - Relational Operators • Operator Meaning Example Value • == equals 2 + 2 == 4 true • != not equals 3.2 != 4.1 true • < less than 4 < 3 false • > greater than 4 > 3 true • <= less than or eql 2 <= 0 false • >= grtr than or eql 2.4 >= 1.6 true • *Note: these are only for primitives! Objects are trickier to compare, and we’ll get there next.
Operator Precedence Revisited • PEMDAS R E A • (R)elational operators are evaluated last, even after addition and subtraction • The (E)quality (==, !=) are even less priority • And the least priority? (A)ssignment (=, +=, *=, …) • All this means is that expressions are computed first, then relationships are determined • X+ 5 < Y //we add 5 to X first, and then check < Y
Uses for Conditional Execution If( numQuizzes > 0 ) { Add up quizzes Divide by non-zero number } If( current < 0) { negatives++; }
Ifs With Fencepost loops • We’ve seen using an if inside a loop (not great) • For(each item) • Print item • If not the last item, print a comma • And an if to guard a loop or division by zero • If( numQuizzes > 0 ) { • For(each quiz) { • sum • } • Divide by number of quizzes, guaranteed to be > 0 • }
Switch Statements • See demo of switch in class…