Chapter 7 Control Statements Continued

Similar documents
Program Correctness and Efficiency. Chapter 2

Checking Multiple Conditions

STUDENT LESSON A12 Iterations

Chapter 4 Introduction to Control Statements

UNIT-4 Black Box & White Box Testing

Testing. UW CSE 160 Winter 2016

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 11, Testing. Using UML, Patterns, and Java. Object-Oriented Software Engineering

UNIT-4 Black Box & White Box Testing

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Software Testing Fundamentals. Software Testing Techniques. Information Flow in Testing. Testing Objectives

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

Topics. Chapter 5. Equality Operators

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Flow of Control: Loops

Unit 1 Lesson 4. Introduction to Control Statements

bool bool - either true or false

Second assignment came out Monday evening. Find defects in Hnefetafl rules written by your classmates. Topic: Code Inspection and Testing

Darshan Institute of Engineering & Technology for Diploma Studies

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

COMP 202 Java in one week

Chapter 4: Control Structures I (Selection)

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Computational Expression

Chapter 5. Conditions, Logical Expressions, and Selection Control Structures

Chapter 3. Selections

Chapter 5 Topics. Chapter 5 Topics. Flow of Control. bool Data Type. Flow of Control. Chapter 5

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

APCS Semester #1 Final Exam Practice Problems

People tell me that testing is

MTAT : Software Testing

Software Testing. Software Testing

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Control Structures: The IF statement!

Flow of Control: Loops. Objectives. Java Loop Statements: Outline 6/27/2014. Chapter 4

What's that? Why? Is one "better" than the other? Terminology. Comparison. Loop testing. Some experts (e.g. Pezze & Young) call it structural testing

Chapter 4: Making Decisions

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Chapter 4: Making Decisions

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

S/W Programming & Languages

4. Flow of Control: Loops. Objectives. Java Loop Statements: Outline 10/3/11. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich

4. Flow of Control: Loops

Software Quality Assurance. David Janzen

Lecture 15 Software Testing

CS 106 Introduction to Computer Science I

Verification and Validation. Assuring that a software system meets a user s needs. Verification vs Validation. The V & V Process

CSE 403: Software Engineering, Fall courses.cs.washington.edu/courses/cse403/16au/ Unit Testing. Emina Torlak

COMP 202. Java in one week

Topics in Software Testing

4. Flow of Control: Loops

4. Flow of Control: Loops. Objectives. Java Loop Statements 10/10/12. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

MTAT : Software Testing

Logic & program control part 3: Compound selection structures

18-642: Unit Testing 1/31/ Philip Koopman

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Why testing and analysis. Software Testing. A framework for software testing. Outline. Software Qualities. Dependability Properties

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Building Java Programs

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Computer Science & Engineering 150A Problem Solving Using Computers

1007 Imperative Programming Part II

Static program checking and verification

CS 520 Theory and Practice of Software Engineering Fall 2018

Software Development Techniques. December Sample Exam Marking Scheme

CMPT 125: Lecture 4 Conditionals and Loops

Quality Assurance in Software Development

Chapter 9. Software Testing

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Lecture 10: Introduction to Correctness

Program Verification! Goals of this Lecture! Words from the Wise! Testing!

Testing: Coverage and Structural Coverage

Topic: Software Verification, Validation and Testing Software Engineering. Faculty of Computing Universiti Teknologi Malaysia

Software Engineering Testing and Debugging Testing

Announcements. Testing. Announcements. Announcements

An Introduction to Systematic Software Testing. Robert France CSU

LECTURE 04 MAKING DECISIONS

Introduction to Programming System Design CSCI 455x (4 Units)

Programming Logic and Design Sixth Edition

a. The following method would allow an object of the static type List<String> to be passed to it as an argument.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Transcription:

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.