Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Boolean Expressions and if1-30-2012Opening DiscussionMinute essay commentsChange a val. REPL vs. ScriptHex 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/3What types of programs will we write?Getting rid of a val.MoreLimit 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.ScriptsWe 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 ExecutionWhen 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 InputScala provides a whole set of functions you can call to read from standard input.readInt()readDouble()readLine()And many moreThis can make your scripts far more useful as they can be used with different values each time you run them.Some ProblemsWe 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 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 works.)Quiz next
View Full Document