Boolean Expressions and if 1 30 2012 Opening Discussion Minute essay comments Change a val REPL vs Script Hex and colors Being proficient fluent in a language Hex letters are for when 4 bits gives a value above 9 Hex vs octal this semester 10 0 3 What types of programs will we write Getting rid of a val More Limit on number of variables Limit on tuples How to get practice Tuples vs arrays Saving REPL sessions Pipe line in REPL How it comes about Matrices in Scala Endangered species keyword was related to jobs Effect of automation on 3rd world workers Scripts We have spent most of our time in the REPL entering one statement at a time When we want to do things repeatedly it is nice to put the commands in a file called a script We can use vi to write a script and put Scala commands in the file We can run it by specifying the file name after the command scala Alternately we can load it into the REPL Sequential Execution When you put commands into a script they are normally executed one after the other from top to bottom This is what we call sequential execution and it is the default way things happen Order can be very significant for the instructions in a program Standard Input Scala provides a whole set of functions you can call to read from standard input readInt readDouble readLine And many more This can make your scripts far more useful as they can be used with different values each time you run them Some Problems We are still a little limited in that we can only do simple math and formatting but let s try to do some things with that in scripts We ll start with formatting money Calculating hourly wages Taking averages of grades Suggestions 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 Quiz next class
View Full Document