Boolean Expressions and if 2 2 2011 Opening Discussion Do you have any questions about the quiz Minute essay comments Solution to quadratic function Start with the math Will we learn if then Higher Order Functions This 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 Execution For 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 if The most basic form of conditional execution is the if The syntax is as follows if condition expr else expr When 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 Statement In 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 Blocks In 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 Comparisons The 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 Example Let 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 Essay When might you want to use conditional execution Hint any time you would use if in a sentence probably works
View Full Document