conditional execution & branching
DESCRIPTION
Conditional Execution & Branching. Instructor Nash Readings: “Cat” Book, Section 3.1. Robert Frost – Mountain interval. Written in 1920. Robert Frost - “The Road Less Taken”. - PowerPoint PPT PresentationTRANSCRIPT
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 IFIf (< 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!
BASIC IF AND IF/ELSE FLOWCHARTS
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
IFS – INVERTED FLUX CAPACITORS?
EVERYTHING MAKES SENSE, YES?
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 SEQUENCEif( 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 EXECUTIONIf( numQuizzes > 0 ) {
Add up quizzesDivide 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…