Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Boolean Expressions and if2-2-2011Opening DiscussionDo you have any questions about the quiz?Minute essay commentsSolution to quadratic function. Start with the math.Will we learn if/then?Higher Order FunctionsThis is the reason function literals exist. These are functions that take other functions as arguments or return other functions.The compose method is higher order.We could write our own compose function that is a higher order function.We will see a lot more higher order functions in chapter 7.Motivating Conditional ExecutionFor my roller skating class I have a component of the grade based on an endurance test where you have to skate for 12 minutes. This component is worth 20 points. The grade you get is 0 for 20 or fewer laps and 20 for 40 or more laps. Between those extremes you get one point for every lap over 20.Calculating this value requires that we do different things in different situations. This is called conditional execution.ifThe most basic form of conditional execution is the if.The syntax is as follows:if(condition) expr else exprWhen Scala gets to an if, it evaluates the condition. The condition is an expression of type Boolean.If the condition is true it evaluates the first expression, otherwise it evaluates the second expression.Expression or StatementIn Scala you can use if as an expression, so it returns a value, or just as a statement where you ignore the value.When used as a statement, the else is optional.Code BlocksIn Scala you can make complex statements or expressions by putting multiple statements inside of curly braces.If it is used as an expression, the value of the expression will be the value of the last expression in it.ComparisonsThe condition needs to be a Boolean expression.The most common basic forms of these are comparisons.Use == and != for equality and non-equality.The ordering comparison operators are <, >, <=, and >=.Coding the ExampleLet's write the code for the skating problem example.Guarding division is another example.What are some other simple examples of places where conditional execution would be helpful?Minute EssayWhen might you want to use conditional execution? (Hint: any time you would use if in a sentence probably
View Full Document