conditional execution & branching

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

Upload: edmund

Post on 24-Feb-2016

43 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Conditional Execution & Branching

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

Page 2: Conditional Execution & Branching

ROBERT FROST – MOUNTAIN INTERVAL

Written in 1920

Page 3: Conditional Execution & Branching

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: Conditional Execution & Branching

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!

Page 5: Conditional Execution & Branching

BASIC IF AND IF/ELSE FLOWCHARTS

Page 6: Conditional Execution & Branching

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: Conditional Execution & Branching

IFS – INVERTED FLUX CAPACITORS?

Page 8: Conditional Execution & Branching

EVERYTHING MAKES SENSE, YES?

Page 9: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

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

Page 13: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

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: Conditional Execution & Branching

USES FOR CONDITIONAL EXECUTIONIf( numQuizzes > 0 ) {

Add up quizzesDivide by non-zero number

}

If( current < 0) {negatives++;

}

Page 20: Conditional Execution & Branching

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: Conditional Execution & Branching

SWITCH STATEMENTS See demo of switch in class…