Conditional Branching If Be Or Not If Be
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. � If stinky… then Mr. Mayewsky takes a shower. � red. Light holds the boolean value for whether or not the light is red � If red. Light… then stop car… otherwise (else) step on it to make the light.
If Else Syntax if (boolean){ …code… //executes if boolean is true }else if (boolean 2){ …code… //executes if boolean if false //and boolean 2 is true }else{ …code… //executes if all booleans are false }
Relational Operators � == � 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!!!!!!!!
If Else Syntax if (time < 7){ mayewsky. sleep(); }else if (time < 16){ mayewsky. work(); }else{ mayewsky. play. Computer. Games(); }
Boolean Operations �! – 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
Order of Operations Example is. Smart || !is. Tall && is. Fast �The expression below will be evaluated in the following order: 1. is. Tall notted by the ! 2. !is. Tall anded with is. Fast by the && 3. is. Smart is ored with !is. Tall && is. Fast by the || �The expression could also be rewritten with parenthesis the following way without changing how it is evaluated: is. Smart || ((!is. Tall) && is. Fast)
Truth Tables 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. A B !A A && B A || B 0 0 1 0 0 0 1 1 1 0 1 1
Truth Tables 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)||B 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1
Simplifying Boolean Expressions �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
Using Boolean Operators if (time < 7 || is. Week. End && time < 10){ mayewsky. sleep(); }else if (time < 16 && !is. Week. End){ mayewsky. work(); }else if(!is. Week. End){ mayewsky. play. Computer. Games(); }else{ mayewsky. watch. Football();
Nesting �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!
If Else Syntax if (is. Week. End)){ if (time < 10){ mayewsky. sleep(); }else{ mayewsky. watch. Football(); } }else{ if (time < 7){ mayewsky. sleep(); }else if (time < 5){ mayewsky. work(); }else{ mayewsky. play. Computer. Games(); } }
- Slides: 13