1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3, 6.1-6.2.

Slides:



Advertisements
Similar presentations
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Advertisements

1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Chapter Day 12. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 12 Problem set corrected  1 A, 2 B’s and 1 D Starting Late and not turning.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Java Translation Program Structure
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
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.
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Switch Statements Comparing Exact Values
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CprE 185: Intro to Problem Solving (using C)
Programming Fundamentals
Control Structures.
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Conditionals and Loops
Conditionals and Loops
The ‘while’ Statement September 27, 2006
Conditionals and Loops
Conditionals and Loops
Conditionals and Loops
Conditionals and Loops
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
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.
Presentation transcript:

1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,

2 Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of certain data types Let's examine some key situations: –Comparing double/float values for equality –Comparing characters –Comparing strings (alphabetical order)

3 Comparing Float Values You should rarely use the equality operator ( == ) when comparing two floating point values ( float or double ) Two floating point values are equal only if their underlying binary representations match exactly Computations often result in slight differences that may be irrelevant In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal

4 Comparing Float Values Your tolerance for equality could be set as follows: To determine the equality of two doubles or floats, use the following technique: If the absolute value of the difference between the two double/float values is less than the tolerance, they are considered to be equal, the if condition is true, and the print statement will execute if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal"); final double TOLERANCE = ;

5 Comparing Characters As we've discussed, Java character data is based on the Unicode character set Unicode assigns a particular numeric value to each character and this creates an ordering of characters We can use relational operators on character data based on this ordering For example, the character ‘A' is less than the character ' J' because it comes before it in the Unicode character set L&L Appendix C provides an overview of Unicode

6 Comparing Characters In Unicode, the digit characters (0-9) are contiguous and in order of their numerical value Likewise, the uppercase letters (A-Z) and lowercase letters (a-z) are contiguous and in alphabetical order CharactersUnicode Values 0 – 948 through 57 A – Z65 through 90 a – z97 through 122

7 Comparing Characters Therefore, if we want to base a decision in our program on whether a character is a digit or not, we can use the following code: if (character >= ‘0’ && character <= ‘9’) System.out.println (“Yes, it’s a digit!”); We can also check for a valid upper case alphabetic character as follows: if (character >= ‘A’ && character <= ‘Z’) System.out.println (“It’s a capital letter!”);

8 Comparing Strings Remember that in Java a string is an object We cannot use the == operator to determine if the values of two strings are identical (character by character) The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order The equals method returns a boolean result if (name1.equals(name2)) System.out.println ("Same name");

9 Comparing Strings We cannot use the relational operators to compare strings The String class contains a method called compareTo to determine if one string comes before another A call to name1.compareTo(name2) –returns zero if name1 and name2 are equal (contain the same characters) –returns a negative value if name1 is less than name2 –returns a positive value if name1 is greater than name2

10 Comparing Strings if (name1.compareTo(name2) < 0) System.out.println (name1 + "comes first"); else if (name1.compareTo(name2) == 0) System.out.println ("Same name"); else System.out.println (name2 + "comes first"); Because comparing characters and strings is based on a character set, it is called a lexicographic ordering

11 Lexicographic Ordering Lexicographic ordering is not strictly alphabetical with mixed uppercase and lowercase characters For example, the string "Great" comes before the string "fantastic" because in Unicode the uppercase letters have lower values than the lowercase letters. Therefore, ‘G’ is less than ‘f’ Also, short strings come before longer strings with the same prefix (lexicographically) Therefore "book" comes before "bookcase"

12 The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an integral expression (int or char only), then attempts to match the result to one of several possible cases Each case contains a value and a statement list The flow of control transfers to the statement list associated with the first case value that matches

13 The switch Statement The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value2, control jumps to here

14 The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we only want to execute the statements associated with one case

15 The switch Statement switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } An example of a switch statement:

16 The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If there is a default case and no other value matches, control will transfer to the default statement list If there is no default case and no other value matches, control falls through to the statement after the switch without executing any statements

17 The switch Statement switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; default: errorCount++; break; } An example of a switch statement using default: