Chapter 7 Control Statements Continued Logical Operators used in Boolean expressions to control behavior of if, while or for statements. && - and, - or,! - not if (the sun shines && you have the time) lets go for a walk; lets stay home; P Sun shining Q Have the time P && Q Outcome if (the sun shines you have the time) lets go for a walk; lets stay home; P Sun shining Q Have the time P Q Outcome if (! The sun shining) Lets stay home; Lets go for walk; P Sunshine!P sun not shine Outcome
if ((sun shines && you have time) it is Sunday) Lets go for a walk; Lets stay home; P Sun shines Q Have time R It is Sunday P && Q (P && Q) R Outcome
if ((sun shines && (you have time it is Sunday)) Lets go for a walk; Lets stay home; P Sun shines Q Have time R It is Sunday Q R P && (Q R) Outcome Which two are equivalent?!(a b) (!a) (!b) (!a) && (!b) demorgan s Law:!(x && y) =!x!y!(x y) =!x &&!y Assume that the variables x and y contain the values 15 and 3, respectively. Indicate if the Boolean expressions below are true, false, or syntactically incorrect. 1) x < y x < y * 6 2) x < y && x < y * 6 3)!x < y 4)!x % y = = 0 5) x && y
Short-circuit evaluation: when the JVM knows the value of a Boolean expression before if has evaluated all of its parts. Once a value can be determined, move on to next statement. Complete evaluation: some programming languages will always evaluate the complete Boolean expression. What will the value of each statement be and which ones cause the Java's shortcircuit evaluation? int a =2, b = 3, c = 4. 6) if ((a < b) && (b < c)) 7) if ((a < b) (b < c)) 8) if ((a > b) && (b > c)) 9) if ((a > b) (b > c)) 10) ( ( ( 2 > 3) (5 > 4) ) &&!(3 <= 5) ) How will the following be evaluated? dog_acceptable = (white black && friendly) Assignment: Make a truth table for each of the following (p && q)!r!(p && (q!r))
Case Study 1: Compute Weekly Pay (page 236-241) Robust handle invalid inputs without crashing or producing meaningless results. Validate data check as soon as entered and reject invalid. User is then given another opportunity to enter a correct value. String methods:.trim() returns a string that contains no leading or trailing spaces.equals() determines it two strings objects contain equal strings. Testing Code with Appropriate Data Quality assurance is the ongoing process of making sure that a software product is developed to the highest standards possible subject to the ever-present constraints of time and money. Complete code coverage means that every line in a program is executed at least once. Unfortunately, this is not the same thing as testing all possible logical paths through a program, which would provide a more thorough test, but might require more test data. Equivalence class is all the sets of test data that exercise a program in the same manner. Equivalent because it tests the same path through a program. Boundary conditions that is, on or near the boundaries between equivalence classes.
Extreme conditions that is, data at the limits of validity. Nested if Statements o Best to make a truth table first, then write code to match. o Instead of a truth table, we could draw a flowchart See insurance problem see overhead Exam exempt problem see overhead Avg >= 90 and absent 3 days or less exempt Avg >= 80 and absent 1 days or less exempt Otherwise not exempt Extended if statement or Multiway if statement: Nested selection in which additional if- statements are used in the. Example: Grades if (testavg >= 90) System.out.println ( A ); if (testavg >=80) System.out.println ( B ); if (testavg >=70) System.out.println ( C ); if (testavg >=60) System.out.println ( D ); System.out.println ( F ); (Also validate data on overhead example) if (conditional) if (conditional) statement; is the same as. if(conditional && conditional) statement;
Logical Errors in Nested If s Misplaced Braces If (the weather is wet) { if (you have an umbrella) walk; run; } if (the weather is wet) { if (you have an umbrella) walk; } run; Remove Braces if (the weather is wet) if (you have an umbrella) walk; run; ***Note: Java pairs the with the closest preceding if ***better to overuse vs. under use braces Do exercise 7.4 page 252 (1-2) When testing a program with loops, it is more challenging to develop a good set of test data. Test data should: o Iterate zero times o Iterate one time o Iterate more than one time
Even small programs can take much data to fully test. We call this combinatorial explosion (a multiplicative growth). Case Study 2: Fibonacci Numbers Loop verification is the process of guaranteeing that a loop performs its intended task, independently of testing. Assert Statement o The purpose of the assert statement is to give you a way to catch program errors early. o An assert statement tells you that you have a bug. o You write assertions to state thing you know (or think you know) about your program. o Input assertions state what should be true before a loop is entered. o Output assertions state what should be true when the loop is exited. o Loop invariant is an assertion that expresses a relationship between variable that remain constant throughout all iterations of the loop. o Loop variant is an assertion whose truth changes between the first and final execution of the loop. Advanced Operations on Strings Note that there are no mutator methods for strings. The reason for this is that strings are immutable objects. Once a string is created, its length cannot change and one cannot modify any of its characters. Using a Scanner with a string. See coding example.
Summary of Testing and Debugging Hints 1. Most errors involving selection statements and loops are NOT syntax errors. You need extensive testing to find them. 2. The presence or absence of braces can seriously affect the logic of a selection statement or loop. 3. When testing programs that use if- statements, be sure to test data that force the program to exercise all of the logical braches. 4. When testing programs that uses if statements, it helps to formulate equivalence classes, boundary conditions, and extreme conditions. 5. Use and if- statement rather than two if statements when the alternative courses of action are mutually exclusive. 6. When testing a loop, be sure to use limit values as well as typical values. 7. Be sure to check entry conditions and exit conditions for each loop. 8. For loops with errors, use debugging output statements to verify the values of the control variable on each pass through the loop. Check this value before the loop is initially entered, after each update, and after the loop is exited.