c onditional e xecution & b ranching instructor nash readings: “cat” book, section 3.1

21
CONDITIONAL EXECUTION & BRANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

Upload: preston-berry

Post on 23-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

CONDITIONAL EXECUTION & BRANCHING

Instructor Nash Readings: “Cat” Book, Section 3.1

Page 2: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

ROBERT FROST – MOUNTAIN INTERVAL

Written in 1920

Page 3: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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.

Page 4: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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!

Page 5: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

BASIC IF AND IF/ELSE FLOWCHARTS

Page 6: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

Page 7: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

IFS – INVERTED FLUX CAPACITORS?

Page 8: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

EVERYTHING MAKES SENSE, YES?

Page 9: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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>

}

Page 10: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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>

}

Page 11: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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?

Page 12: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

Page 13: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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)

Page 14: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

}

Page 15: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

Page 16: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

Page 17: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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.

Page 18: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

Page 19: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

USES FOR CONDITIONAL EXECUTION

If( numQuizzes > 0 ) {Add up quizzesDivide by non-zero number

}

If( current < 0) {negatives++;

}

Page 20: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

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

}

Page 21: C ONDITIONAL E XECUTION & B RANCHING Instructor Nash Readings: “Cat” Book, Section 3.1

SWITCH STATEMENTS

See demo of switch in class…