Week 4 - Decisions

Week 4 Decisions

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ;= 5) •  !(number <= 5) •  !(number == 5) •  !(number > 5) •  !(number != 5) 24 How to avoid using not What is the equivalent expression of the following: •  !(number < 5) (number >= 5) •  !(number <= 5) (number > 5) •  !(number > 5) (number <= 5) •  !(number >= 5) (number < 5) •  !(number == 5) (number != 5) •  !(number != 5) (number == 5) 25 Summary of boolean operators The effects of boolean operators on boolean values A true true false false B true false true false A && B true false false false 26 A || B true true true false !(A) false false true true Using == •  The equality operator (==) is used for comparing –  int –  boolean –  char •  Do not use equality operator (==) to compare –  Strings –  float –  double 27 Problem with Floating Point Numbers double firstNum = 2.00; double secondNum = 1.10; double answer = 0.0; answer = firstNum - secondNum; //2.00 – 1.10 System.out.println("Is answer = 0.9?"); if (answer == 0.9) { System.out.println("Yes!"); } else { System.out.println("No!"); } 28 Solution double firstNum = 2.00; double secondNum = 1.10; double answer = 0.0; answer = firstNum - secondNum; //2.00 – 1.10 System.out.println("Is answer = 0.9?"); if ((answer > 0.89) && (answer < 0.91)) { System.out.println("Approximately"); } else { System.out.println("No"); } 29 Equality with oats •  The equality operator (==) is inappropriate for float or double –  These types store approximations not exact values like int •  Determine a tolerance and use that answer –...
View Full Document

This note was uploaded on 02/17/2014 for the course ITP 109 taught by Professor Trinagregory during the Spring '12 term at USC.

Ask a homework question - tutors are online