Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Topic 17 assertions and program logic
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
True/False. False True Subject May Go Here True / False ? Type correct answer here. Type incorrect answer here.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
1 Logical Assertions. 2 Logical assertions assertion: A statement that is either true or false. Examples: –Java was created in –The sky is purple.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
CS 112 Introduction to Programming Program Analysis; Fencepost Loops Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CS0007: Introduction to Computer Programming
4. Java language basics: Function
Strings, conditional statements, Math
The switch Statement, and Introduction to Looping
Debugging and Random Numbers
Computer Programming If Statements Trade & Industrial Education
Midterm Review Problems
Building Java Programs
Control Statement Examples
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Conditional Branching
Conditionals & Boolean Expressions
Coding Concepts (Sub- Programs)
Building Java Programs
Topic 17 assertions and program logic
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Java Programming Control Structures Part 1
Module 4 Loops.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Quadratic Inequalities
Quadratic Inequalities
Building Java Programs
Conditional Logic Presentation Name Course Name
Building Java Programs
Building Java Programs
Building Java Programs
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
CMSC 202 Exceptions 2nd Lecture.
How do I solve a proportion?
Building Java Programs
Building Java Programs
Building Java Programs
CS 101 First Exam Review.
Building Java Programs
Indefinite loop variations
Building Java Programs
Building Java Programs
Building Java Programs
11.6 Systems of Equations.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
CMSC 202 Exceptions 2nd Lecture.
This shows running the first guess number program which only allows one guess - I show the correct answer for testing purposes.
Presentation transcript:

Logical assertions assertion: A statement that is either true or false. Examples: Java was created in 1995. The sky is purple. 23 is a prime number. 10 is greater than 20. x divided by 2 equals 7. (depends on the value of x) An assertion might be false ("The sky is purple" above), but it is still an assertion because it is a true/false statement.

Reasoning about assertions Suppose you have the following code: if (x > 3) { // Point A x--; } else { // Point B x++; // Point C } // Point D What do you know about x's value at the three points? Is x > 3? Always? Sometimes? Never?

Assertions in code We can make assertions about our code and ask whether they are true at various points in the code. Valid answers are ALWAYS, NEVER, or SOMETIMES. System.out.print("Type a nonnegative number: "); double number = console.nextDouble(); // Point A: is number < 0.0 here? while (number < 0.0) { // Point B: is number < 0.0 here? System.out.print("Negative; try again: "); number = console.nextDouble(); // Point C: is number < 0.0 here? } // Point D: is number < 0.0 here? (SOMETIMES) (ALWAYS) (NEVER)

Reasoning about assertions Right after a variable is initialized, its value is known: int x = 3; // is x > 0? ALWAYS In general you know nothing about parameters' values: public static void mystery(int a, int b) { // is a == 10? SOMETIMES But inside an if, while, etc., you may know something: if (a < 0) { // is a == 10? NEVER ... }

"Sometimes" Things that cause a variable's value to be unknown (often leads to "sometimes" answers): reading from a Scanner reading a number from a Random object a parameter's initial value to a method If you can reach a part of the program both with the answer being "yes" and the answer being "no", then the correct answer is "sometimes". If you're unsure, "Sometimes" is a good guess.