ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Logic & program control part 2: Simple selection structures.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 5: Making Decisions
ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Decision Structures, String Comparison, Nested Structures
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
ITP © Ron Poet Lecture 5 1 Branching. ITP © Ron Poet Lecture 5 2 CookTime After Midnight  We want to improve our program so that we can cook meals after.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
 Type Called bool  Bool has only two possible values: True and False.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter 2 Basic Computation
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
Lecture 3- Decision Structures
Multiple variables can be created in one declaration
Variables and Primative Types
Incrementing ITP © Ron Poet Lecture 8.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter 3: Program Statements
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Boolean Expressions to Make Comparisons
CprE 185: Intro to Problem Solving (using C)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Building Java Programs
Presentation transcript:

ITP © Ron Poet Lecture 6 1 More on if

ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use it later on.  We might work out if a year is a leap year or not.  The formula is quite complicated.  If we could remember the result of the test, we would not have to keep applying the complicated formula.

ITP © Ron Poet Lecture 6 3 boolean Data Type  The boolean data type has two possible values, true and false.  These are boolean literals.  Any test is a boolean expression, with possible values true and false.  We can store boolean values in boolean variables.

ITP © Ron Poet Lecture 6 4 Storing Test Results in Variables  We can store our previous test results in boolean variables. boolean nightFeast = startTotalMins < 0; boolean itsRon = name.equals(“Ron”);  They are either true or false.  There are just examples, it would not be good style to actually do this in these cases.

ITP © Ron Poet Lecture 6 5 Using boolean Variables in Tests  We can use these stored values in tests.  As part of an if statement. if (nightFeast) startTotalMins = startTotalMins ; if (isRon) con.println(“Why, Hello Ron”);  Using the tests later is often useful.  But not in these simple examples.

ITP © Ron Poet Lecture 6 6 Leap Years  The rules for deciding if a year is a leap year are complicated.  Making it a useful example.  A year is a leap year if:  It is divisible by 4.  But if it is divisible by 100 it is not a leap year.  Unless it is also divisible by 400.

ITP © Ron Poet Lecture 6 7 Leap Year Calculation  Let us try and calculate if a year is leap, storing the result in the boolean variable isLeapYear.  We can use this stored value later on to calculate how many days there are in February of that year.

ITP © Ron Poet Lecture 6 8 Chaining if Statements  Remember, make the most restrictive test first. Boolean isLeapYear; if (year % 400 == 0)// divisible by 400 isLeapYear = true; else if (year % 100 == 0) // divisible by 100 isLeapYear = false; else if (year % 4 == 0) // divisible by 4 isLeapYear = true; else isLeapYear = false;

ITP © Ron Poet Lecture 6 9 Nested if Statements  We can go straight from the definition, leading to a less elegant solution. Boolean isLeaYear; if (year % 4 == 0) if (year % 100 == 0) if (year % 400 == 0) isLeapYear = true; else isLeapYear = false; else isLeapYear = true; else isLeapYear = false;

ITP © Ron Poet Lecture 6 10 Days in The Month  Whichever approach we take, we now have a variable called isLeapYear that is either true or false.  Now let us assume we have a String variable month which stores the 3 letter abbreviation of the month name.  We want to calculate int daysInMonth.

ITP © Ron Poet Lecture 6 11 Twelve Tests  The obvious solution is the rather long series of tests. if (month.equals(“Jan”)) daysInMonth = 31; else if (month.equals(“Feb”)) if (isLeapYear) daysInMonth = 29; else daysInMonth = 28; else if (month.equals(“Mar”)) daysInMonth = 31;...

ITP © Ron Poet Lecture 6 12 Reflection  Note how we have stored the result of a test in a boolean variable.  The boolean variable is then used in place of the test in an if statement.  Note that things can get complicated and we have to keep track of which if the else parts belong to.  Indentation is vital.

ITP © Ron Poet Lecture 6 13 Combining Tests  The previous two examples have used multiple tests to calculate whether a year is leap and the days in a month.  It would be convenient if we could combine tests to produce super-tests.  Rather than using lots of if statements with simple tests.

ITP © Ron Poet Lecture 6 14 Boolean Operators  Boolean operators combine tests to produce super-tests.  These super-tests still have possible values of true or false.  They can be used like any of the simpler tests.  We can combine super-tests to produce even bigger tests, as far as we want.

ITP © Ron Poet Lecture 6 15 Test AND Test  If we combine two tests with the AND operator then the result is only true if both the simple tests are true.  If one test is false then the result is false, no matter what the result of the other test is.  The Java notation for AND is &&.

ITP © Ron Poet Lecture 6 16 Test OR Test  If we combine two tests with the OR operator then the result is true if only one of the simple tests is true.  It is only false if both the simple tests are false.  The Java notation for OR is ||.  It has the lowest precedence of the boolean operators.

ITP © Ron Poet Lecture 6 17 NOT Test  The NOT operator switches the value of the test.  If the test were true then NOT test is false.  If the test were false then NOT test is true.  The Java notation for NOT is !  It has the highest precedence of the boolean operators.

ITP © Ron Poet Lecture 6 18 Days in the Month Again  This makes the days in the month code easier.  It fits on one page.  Note that we combine the month test with OR. Only one test will ever be true!

ITP © Ron Poet Lecture 6 19 Days in the Month Code if (month.equals(“Jan”) || month.equals(“Mar”) || month.equals(“May”) || month.equals(“Jul”) || month.equals(“Aug”) || month.equals(“Oct”) || month.equals(“Dec”)) daysInMonth = 31; else if (month.equals(“Apr”) || month.equals(“Jun”) || month.equals(“Sep”) || month.equals(“Nov”)) daysInMonth = 30; else// must be Feb if (isLeapYear) daysInMonth = 29; else daysInMonth = 28;

ITP © Ron Poet Lecture 6 20 Leap Years Again  The leap year code is also complicated.  We must make sure we have the right combinations of AND and OR.  Use test data. Year = 3, 4, 100, 400 covers all the possibilities.

ITP © Ron Poet Lecture 6 21 Leap Year Code if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) isLeapYear = true; else isLeapYear = false;

ITP © Ron Poet Lecture 6 22 Testing in a Range  Suppose we want a test that is true if an integer x is between 37 and 39.  We must make two tests.  x >= 37, bigger than the lower value.  x <= 39, smaller than the higher value.  Join them with AND, both tests must be true.  if (x >= 37 && x <= 39).

ITP © Ron Poet Lecture 6 23 Alphabetical Order  We might want to test two strings to see which one comes first in the alphabet.  Use compareTo rather than equals.  a.compareTo(b) returns an int.  If the value < 0 then a comes before b.  If the value > 0 then a comes after b.  If the value == 0 then a and b are equal.

ITP © Ron Poet Lecture 6 24 Dangling else  If we have two if s but only one else.  The else belongs to the nearest if. if (test1) if (test2) // else belongs to this one statement; else statement;  Unless that is not possible because of {}.

ITP © Ron Poet Lecture 6 25 Dangling else and {} if (test1) // else belongs to this if { if (test2) // statement; } else statement;

ITP © Ron Poet Lecture 6 26 doubles Are Not Stored Exactly  Real numbers ( double ) are not normally stored exactly.  They are stored very accurately, to around 17 significant figures.  A variable that we expect to hold the value 3.0 might actually hold  This is good enough for most purposes.  int s are stored exactly.

ITP © Ron Poet Lecture 6 27 Comparing doubles  If we compare a variable x with the value 3.0 then the result might be false because x actually contained  We should not write  if (x == 3.0)  We should instead subtract 3.0 from x and see if the number is very small.

ITP © Ron Poet Lecture 6 28 Close Enough  If x is close enough to 3.0, then x – 3.0 will be a small number, either positive or negative.  We should take the absolute value of x – 3.0  which is always positive. if (x > 3.0) absxmin3 = x – 3.0; else absxmin3 = x;  It is better to use java.lang.Math.abs(x – 3.0).

ITP © Ron Poet Lecture 6 29 Close Enough (2)  A typical small value is 1.0e-10.  So the test to see if x is close enough to 3.0 would be if (java.lang.Math.abs(x – 3.0) < 1.0e-10)