CSS161: Fundamentals of Computing

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Logical Operators and Conditional statements
Chapter 4 Making Decisions
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 4: Basic C Operators
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Comp 248 Introduction to Programming Chapter 3 – Flow of Control Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Flow of Control Part 1: Selection
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Java-02 Basic Concepts Review concepts and examine how java handles them.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
CompSci 230 S Programming Techniques
Chapter 3 Flow of Control
Chapter 3 Flow of Control
Expressions and Assignment Statements
CprE 185: Intro to Problem Solving (using C)
Chapter 2 Basic Computation
Chapter 7: Expressions and Assignment Statements
Boolean expressions and if-else statements
University of Central Florida COP 3330 Object Oriented Programming
Computing Fundamentals
Java Primer 1: Types, Classes and Operators
Chapter 3 Edited by JJ Shepherd
University of Central Florida COP 3330 Object Oriented Programming
EGR 2261 Unit 4 Control Structures I: Selection
Relational Operations
Chapter 7: Expressions and Assignment Statements
Chapter 3 Branching Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Intro to C Tutorial 4: Arithmetic and Logical expressions
Boolean Expressions and If
Program Style Console Input and Output
CSS161: Fundamentals of Computing
Logical Operators & Truth Tables.
Data Types, Identifiers, and Expressions
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 8 JavaScript: Control Statements, Part 2
Fundamentals 2.
CMSC 202 Java Primer 2.
Introduction to Programming – 4 Operators
Expressions and Assignment Statements
Expressions and Assignment
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3 Flow of Control
CSS161: Fundamentals of Computing
Classes, Objects and Methods
CSC 1051 – Data Structures and Algorithms I
CSS161: Fundamentals of Computing
Controlling Program Flow
Boolean Expressions September 1, 2019 ICS102: The course.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

CSS161: Fundamentals of Computing Boolean Expressions This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

Simple Boolean Expressions A Boolean expression is an expression that returns either true or false. The simplest expressions are comparisons of two expressions: time < limit balance <= 0 If a Boolean expression is used in an if-else statement, it must be enclosed in parentheses. if ( time < limit ) CSS161: Fundamentals of Computing

Java Comparison Operators CSS161: Fundamentals of Computing

PITFALL 1: Using = in Place of == Comparison of two integers Correct int yourScore, myScore; ...; // yourScore, myScore initialized if ( yourScore == myScore ) System.out.println( “A tie” ); Incorrect if ( yourSocre = myScore) Compilation error: incompatible types found : int required: boolean if ( yourScore = myScore ) ^ Comparison of two booleans Correct boolean passedCSS161 = false; ...; // passedCSS161 modified if ( passedCSS161 == true ) System.out.println( “passed” ); Incorrect if ( passedCSS161 = true ) No Compilation error! It’s always true. CSS161: Fundamentals of Computing

PITFALL 2: Using == with Strings Incorrect String greetings; Scanner keyboard = new Scanner( System.in ); greetings = keyboard.nextLine( ); if ( greetings = “How are you?” ) System.out.println( “Fine, thank you.” ) Output: no outputs Correct if ( greetings.equals( “How are you?” ) ) System.out.println( “Fine, thank you.” ); Output: Fine, thakn you. == in Strings compare their references. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing ASCII Code 32 56 8 80 P 104 h 33 ! 57 9 81 Q 105 iI 34 “ 58 : 82 R 106 j 35 # 59 ; 83 S 107 k 36 $ 60 < 84 T 108 l 37 % 61 = 85 U 109 m 38 & 62 > 86 V 110 n 39 ‘ 63 ? 87 W 111 o 40 ( 64 @ 88 X 112 p 41 ) 65 A 89 Y 113 q 42 * 66 B 90 Z 114 r 43 + 67 C 91 [ 115 s 44 , 68 D 92 \ 116 t 45 - 69 E 93 ] 117 u 46 . 70 F 94 ^ 118 v 47 / 71 G 95 _ 119 w 48 72 H 96 ` 120 x 49 1 73 I 97 a 121 y 50 2 74 J 98 b 122 z 51 3 75 K 99 c 123 { 52 4 76 L 100 d 124 | 53 5 77 M 101 e 125 } 54 6 78 N 102 f 126 ~ 55 7 79 O 103 g 127 CSS161: Fundamentals of Computing

Alphabetical and Lexicographic Order Alphabetical Order: A(a), B(b), C(c), …, Z(z) Lexicographic Order: based on ASCII CODE String has three methods: string1.equals( string2 ) Returns true if string1 matches string2 exactly, otherwise false. string1.compareTo( string2 ) Based on lexicographic order Returns 0 if string1 matches string2 exactly, a positive integer if string1 comes before string2, otherwise a negative integer string1.compareToIgnore( string2 ) Based on alphabetical order (in uppercase letters) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example String string1 = “abcd”; String string2 = “abaf”; System.out.println( string1.compareTo( string2 ); // ‘a’ – ‘a’ = 0; ‘b’ – ‘b’ = 0; ‘c’ – ‘d’ = 2; thus return 2. // Note that ‘d’ – ‘f’ = -2 but this comparison is ignored. String string3 = “abcd”; String string4 = “ABCD”; System.out.println( string3.compareTo( string4 ); // ‘a’- ‘A’ = -32; thus return -32. System.out.println( string3.compareToIgnore( string4 ); // All letters are compared in uppercase. Thus return 0. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook P111’s exercises 13 ~ 16. CSS161: Fundamentals of Computing

Building Boolean Expressions Two Boolean operators to combine Boolean expressions: AND ( expression1 && expression2 ): returns true if both expressions are true. ( number > 2 ) && ( number < 7 ) // will be true if number is 3, 4, 5, 6 OR ( expression1 || expression2 ): returns true if either one of them is true. ( count < 3 ) || ( count > 12 ) // will be true if count is …, -2, -1, 0, 1, 2, 13, 14, 15, 16, … Negation( ! ) used to negate a given expression: !expression1: returns true if it is false. ! (savings < debt ) // true if savings >= debt CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Truth Tables CSS161: Fundamentals of Computing

Evaluating Boolean Expressions !( ( count < 3 ) || ( count > 7 ) ) What if count is 8? ( number > 0 ) && !( number % 2 == 0 ) && !( number % 3 == ) What numbers make this expression true? !( data < 0 ) && ( ( data / 100 < 1 ) || ( data % 100 == 0 ) ) What data make this expression true? CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook p114’s exercises 17 ~ 18. CSS161: Fundamentals of Computing

Short-Circuit or Lazy Evaluation expression1 && expression2 && expression3 False && false && false returns false False && false && true returns false False && true && true returns false Thus, if expression1 is false, no evaluation on expressions 2 and 3. expression1 || expression2 || expression3 True || true || true returns true True || true || false returns true True || false || false returns true Thus, if expression1 is true, no evaluation on expressions 2 and 3. Useful to prevent a runtime error if ( ( kids != 0 ) && ( ( pieces / kids ) >= 2 ) ) System.out.println( “Each child may have two pieces!” ); Complete evaluation Use & and | instead of && and ||. CSS161: Fundamentals of Computing

Precedence and Associativity Rules CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Questions Include parentheses in each of the following expressions to keep their operator precedence. bonus + balance * rate / correctionFactor – penalty number1 = number2 = number3 + 7 * factor number + 1 > 2 || number + 5 < -3 expenses < income || expenses < savings || creditRating > 0 Recommended parentheses rule: include most parentheses, except where the intended meaning is obvious. CSS161: Fundamentals of Computing

Side Effects and Evaluation Rule Side Effects: when, in addition to returning a value, an expression changes something, such as the value of a variable The assignment, increment, and decrement operators all produce side effects Evaluation Rule: Perform binding: Determine the equivalent fully parenthesized expression using the precedence and associativity rules Proceeding left to right, evaluate whatever subexpressions can be immediately evaluated These subexpressions will be operands or method arguments, e.g., numeric constants or variables Evaluate each outer operation and method invocation as soon as all of its operands (i.e., arguments) have been evaluated CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises ( ( result = ( ++n ) + ( other = ( 2 * ( ++n ) ) ) ) What if n is 2 at first? ( ++n > 0 ) && ( 5 > n ) What if n is 0 at first? Work on Textbook p125’s exercises 19 ~ 21. CSS161: Fundamentals of Computing