Chapter 6 Control Statements Continued

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Describing Syntax and Semantics
Chapter 6 Control Statements Continued
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
1 Chapter 7 Control Statements Continued Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Logic and Design Sixth Edition
Chapter 3 Making Decisions
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 4 Introduction to Control Statements Fundamentals of Java.
Decision Making and Branching (cont.)
Chapter 4 Introduction to Control Statements
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 4: Control Structures I (Selection)
Chapter 4 – C Program Control
Section 7.1 Logical Operators
Loop Structures.
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Control Statements I
A First Book of ANSI C Fourth Edition
Chapter 6 Control Statements: Part 2
Chapter 6: Repetition Statements
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Controlling Program Flow
Presentation transcript:

Chapter 6 Control Statements Continued Fundamentals of Java

Fundamentals of Java

Fundamentals of Java

Fundamentals of Java

Fundamentals of Java

Homework Read 6.1, 6.2 Complete Exercises 6.1, 6.2 Fundamentals of Java

Homework Read 6.3, 6.4 Complete Exercises 6.3, 6.4 Fundamentals of Java

Objectives Construct complex Boolean expressions using the logical operators && (AND), || (OR), and ! (NOT). Construct truth tables for Boolean expressions. Understand the logic of nested if statements and extended if statements. Fundamentals of Java

Objectives (cont.) Test if statements in a comprehensive manner. Construct nested loops. Create appropriate test cases for if statements and loops. Understand the purpose of assertions, invariants, and loop verification. Fundamentals of Java

Vocabulary Arithmetic overflow Boundary condition Combinatorial explosion Complete code coverage Equivalence class Extended if statement Fundamentals of Java

Vocabulary (cont.) Extreme condition Input assertion Logical operator Loop invariant Loop variant Nested if statement Fundamentals of Java

Vocabulary (cont.) Nested loop Output assertion Quality assurance Robust Truth table Fundamentals of Java

Logical Operators Used in Boolean expressions that control the behavior of if, while, and for statements Three logical operators: AND OR NOT Can combine smaller logical expressions into larger ones Fundamentals of Java

Logical Operators (cont.) Truth tables: For complex logical expressions, shows how value of overall condition depends on values of operands All combinations of values considered 2n combinations of true and false for n operands Fundamentals of Java

Logical Operators (cont.) Table 6-1: Truth tables for three example sentences Fundamentals of Java

Logical Operators (cont.) Table 6-2: General rules for AND, OR, and NOT Fundamentals of Java

Logical Operators (cont.) Can use parentheses with complex expressions If (the sun is shining AND it is 8 a.m.) OR (NOT your brother is visiting) then let’s go for a walk else let’s stay at home. If the sun is shining AND (it is 8 a.m. OR (NOT your brother is visiting)) then let’s go for a walk else let’s stay at home. Truth tables would clarify these statements. Fundamentals of Java

Logical Operators (cont.) Java’s logical operators: AND: && OR: || NOT: ! Precedence rules: ! has same precedence as other unary operators. && and || only have higher precedence than the assignment operators. && has higher precedence than ||. Fundamentals of Java

Logical Operators (cont.) Example: Fundamentals of Java

Logical Operators (cont.) Boolean variables: Can have value of true or false Primitive data type Example: boolean b1 = true; if ( b1 ) { <do something> } Fundamentals of Java

Logical Operators (cont.) Can break down complex Boolean expressions into a series of simpler ones Useful equivalences: !(p || q)  !p && !q !(p && q) !p || !q p || (q && r) (p || q) && (p || r) p && (q || r)  (p && q) || (p && r) Fundamentals of Java

Logical Operators (cont.) Short-circuit evaluation: JVM may know result of a Boolean expression before evaluating all of its parts. Stops evaluation as soon as result is known Example: if( true || false ) Entire condition is true because first operand of the Boolean expression is true. Second operand not examined at all Fundamentals of Java

Testing if Statements Quality assurance: Ongoing process of making sure that a software product is developed to the highest standards possible Subject to the constraints of time and money Test data should try to achieve complete code coverage. Every line in a program executed at least once Fundamentals of Java

Testing if Statements (cont.) All sets of test data that exercise a program in the same manner belong to same equivalence class. Test same paths through the program Test data should include cases that assess program’s behavior under boundary conditions. On or near boundaries between equivalence classes Fundamentals of Java

Testing if Statements (cont.) Test under extreme conditions With data at the limits of validity Test data validation rules Enter values that are valid and invalid. Test boundary values between the two. Fundamentals of Java

Nested if Statements Placing if statements within other if statements Alternative to using logical operators Fundamentals of Java

Nested if Statements (cont.) A nested if statement can be translated to an equivalent if statement that uses logical operators. The reverse is also true. Table 6-6: Truth table for reading a book, watching TV, or going for a walk Fundamentals of Java

Nested if Statements (cont.) Figure 6-4: Flowchart for reading a book, watching TV, or going for a walk Fundamentals of Java

Nested if Statements (cont.) Example: Fundamentals of Java

Nested if Statements (cont.) Extended if statement: Fundamentals of Java

Logical Errors in Nested if Statements Misplaced braces example: Fundamentals of Java

Logical Errors in Nested if Statements (Cont.) Table 6-7: Truth table for version 1 and version 2 Fundamentals of Java

Logical Errors in Nested if Statements (cont.) If braces removed, Java pairs the else with closest preceding if. Can create logic or syntax errors if not careful Can you spot the error in the following code? Fundamentals of Java

Logical Errors in Nested if Statements (cont.) Better to over-use braces than under-use Order of conditions often important in extended if-else constructs Can you spot the logic error? Fundamentals of Java

Logical Errors in Nested if Statements (cont.) Avoiding nested if statements can improve clarity, as in: if (90 <= average ) grade is A; if (80 <= average && average < 90) grade is B; if (70 <= average && average < 80) grade is C; if (60 <= average && average < 70) grade is D; if ( average < 60) grade is F; Fundamentals of Java

Nested Loops A loop within another loop Example: Fundamentals of Java

Testing Loops Loops increase difficulty of testing. Often do not iterate some fixed number of times Design test data to cover situation where loop iterates: Zero times One time Multiple times Fundamentals of Java

Table 6-10: Test data for the count divisors program Testing Loops (cont.) Table 6-10: Test data for the count divisors program Fundamentals of Java

Testing Loops (cont.) Combinatorial Explosion: Creating test data to verify multiple dependant components can result in huge amount of test data. Example: 3 dependent components, each of which requires 5 tests to verify functionality Total number of tests to verify entire program is 5*5*5=125. Fundamentals of Java

Testing Loops (cont.) Robust program: Tolerates errors in user inputs and recovers gracefully Best and easiest way to write robust programs is to check user inputs immediately on entry. Reject invalid user inputs. Fundamentals of Java

Loop Verification Process of guaranteeing that a loop performs its intended task Independently of testing assert statement: Allows programmer to evaluate a Boolean expression and halt the program with an error message if the expression’s value is false General form: assert <Boolean expression> Fundamentals of Java

Loop Verification (cont.) To enable when running the program: java -enableassertions AJavaProgram Example 6.1: Assert that x != 0 Fundamentals of Java

Loop Verification (cont.) Figure 6-6: Failure of an assert statement Fundamentals of Java

Loop Verification: Assertions with Loops Input assertions: State what can be expected to be true before a loop is entered Output assertions: State what can be expected to be true when the loop is exited Table 6-12: Sums of the proper divisors of some integers Fundamentals of Java

Loop Verification: Assertions with Loops (cont.) Sum proper divisors of positive integer: Input assertions: num is a positive integer divisorSum == 0 Output assertion: divisorSum is sum of all proper divisors of num Fundamentals of Java

Loop Verification: Invariant and Variant Assertions Loop invariant: Assertion that expresses a relationship between variables that remains constant throughout all loop iterations Loop variant: Assertion whose truth changes between the first and final iteration Stated so that it guarantees the loop is exited Usually occur in pairs Fundamentals of Java

Loop Verification: Invariant and Variant Assertions (cont.) Fundamentals of Java

Graphics and GUIs: Timers and Animations In animation, slightly different frames are displayed in order at a rapid rate. Produces illusion of continuous movement Moving objects have: Velocity Distance (in pixels) traveled in a given unit of time Direction Fundamentals of Java

Graphics and GUIs: Timers and Animations (cont.) Figure 6-7: Representing directions in two dimensions Fundamentals of Java

Graphics and GUIs: Timers and Animations (cont.) Algorithm for animating a graphical object: Java provides a timer object to schedule events at regular intervals. Fundamentals of Java

Graphics and GUIs: Timers and Animations (cont.) Timer for animations is an instance of the class Timer. Fundamentals of Java

Graphics and GUIs: Timers and Animations (cont.) Table 6-15: Some commonly used Timer methods Fundamentals of Java

Design, Testing, and Debugging Hints Most errors involving selection statements and loops are not syntax errors caught at compile time. The presence or absence of braces can seriously affect the logic of a selection statement or loop. Fundamentals of Java

Design, Testing, and Debugging Hints (cont.) When testing programs with if or if-else statements, use data that force the program to exercise all logical branches. When testing programs with if statements, formulate equivalence classes, boundary conditions, and extreme conditions. Use an if-else statement rather than two if statements when the alternative courses of action are mutually exclusive. Fundamentals of Java

Design, Testing, and Debugging Hints (cont.) When testing a loop, use limit values as well as typical values. Check entry and exit conditions for each loop. For a loop with errors, use debugging output statements to verify the control variable’s value on each pass through the loop. Check value before the loop is initially entered, after each update, and after the loop is exited. Fundamentals of Java

Summary A complex Boolean expression contains one or more Boolean expressions and the logical operators && (AND), || (OR), and ! (NOT). A truth table can determine the value of any complex Boolean expression. Java uses short-circuit evaluation of complex Boolean expressions. Fundamentals of Java

Summary (cont.) Nested if statements are another way of expressing complex conditions. A nested if statement can be translated to an equivalent if statement that uses logical operators. An extended or multiway if statement expresses a choice among several mutually exclusive alternatives. Fundamentals of Java

Summary (cont.) Loops can be nested in other loops. Equivalence classes, boundary conditions, and extreme conditions are important features used in tests of control structures involving complex conditions. Loops can be verified to be correct by using assertions, loop variants, and loop invariants. Fundamentals of Java