conditional branching

13

Upload: desmond-clear

Post on 04-Jan-2016

76 views

Category:

Documents


0 download

DESCRIPTION

Conditional Branching. If Be Or Not If Be (then else). booleans hold a true/false value We take advantage of this by using them to decide which route our program will take. Examples: stinky holds the boolean value for whether or not Mr. Mayewsky is stinky. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Conditional Branching
Page 2: Conditional Branching

booleans hold a true/false valueWe take advantage of this by using them

to decide which route our program will take.

Examples:stinky holds the boolean value for whether or

not Mr. Mayewsky is stinky.If stinky… then Mr. Mayewsky takes a shower.redLight holds the boolean value for whether

or not the light is redIf redLight… then stop car… otherwise (else)

step on it to make the light.

Page 3: Conditional Branching

if (boolean){…code… //executes if boolean is true

}else if (boolean2){…code… //executes if boolean if false

//and boolean2 is true}else{

…code… //executes if all booleans are false

}

Page 4: Conditional Branching

== equal to !=not equal to > greater than >= greater than or equal to < less than <= less than or equal to **Only use these with primitive

variables!!!!!!!!!!!!!!!!

Page 5: Conditional Branching

if (time < 7){mayewsky.sleep();

}else if (time < 16){mayewsky.work();

}else{mayewsky.playComputerGames();

}

Page 6: Conditional Branching

! – the not operator which will negate the boolean value(true becomes false and vice versa)

&& – the and operator which will and two boolean values (both need to be true for the result to be true)

|| – the or operator which will or two boolean values (if either is true then the result is true)

Order of Operation: ! then && then ||Expressions in parenthesis are evaluated first

Page 7: Conditional Branching

isSmart || !isTall && isFast The expression below will be evaluated in the following order:1.isTall notted by the !2.!isTall anded with isFast by the &&3.isSmart is ored with !isTall && isFast by the ||The expression could also be rewritten with parenthesis the following way without changing how it is evaluated:

isSmart || ((!isTall) && isFast)

Page 8: Conditional Branching

A B !A A && B A || B

0 0 1 0 0

0 1 1 0 1

1 0 0 0 1

1 1 0 1 1

You can show the results of a boolean expression using a truth table such as the one below that lists all the possible inputs and outputs. 1’s represent true and 0’s represent false.

Page 9: Conditional Branching

When given a complicated boolean expression such as the one below, you can break it down into components to more easily evaluate the results.

(!A||B) && !(A&&C) || B

A B C !A !A||B

A&&C

!(A&&C)

(!A||B)&&!(A&&C)

(!A||B)&&!(A&&C)||B

0 0 0 1 1 0 1 1 1

0 0 1 1 1 0 1 1 1

0 1 0 1 1 0 1 1 1

0 1 1 1 1 0 1 1 1

1 0 0 0 0 0 1 0 0

1 0 1 0 0 1 0 0 0

1 1 0 0 1 0 1 1 1

1 1 1 0 1 1 0 0 1

Page 10: Conditional Branching

Many times you can simplify a boolean expression.

There are several tactics that you can take such as truth tables. For example, the expression that is evaluated on the previous slide can be simplified down to

!A||B

Page 11: Conditional Branching

if (time < 7 || isWeekEnd && time < 10){mayewsky.sleep();

}else if (time < 16 && !isWeekEnd){mayewsky.work();

}else if(!isWeekEnd){mayewsky.playComputerGames();

}else{mayewsky.watchFootball();

Page 12: Conditional Branching

You can put an if statement inside another if statement

This can be used to better organize your code.

It makes it both easier to write, read, and edit!

Page 13: Conditional Branching

if (isWeekEnd)){if (time < 10){

mayewsky.sleep();}else{

mayewsky.watchFootball();}

}else{ if (time < 7){

mayewsky.sleep();}else if (time < 5){

mayewsky.work();}else{

mayewsky.playComputerGames();}

}