Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.

Slides:



Advertisements
Similar presentations
Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein.
Advertisements

Logic & program control part 2: Simple selection structures.
DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
String and Object Comparison Comparison operator.
C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.
Math 130 Decisions II Wed, Sept 12, 2007 Notes: ref. C. Horstmann, Java Concepts.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Chapter 5  Decisions 1 Chapter 5 Decisions. Chapter 5  Decisions 2 Chapter Goals  To be able to implement decisions using if statements  To understand.
If Statement if (amount
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
Chapter 5: Decisions. To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare.
Chapter 6 - Decisions. Characters char char primitive data type primitive data type stores a single character stores a single character operations (same.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 5 Decisions. Assignments  Written exercises pp 217 – 220 R5.1, R5.3, R5.5, R5.6, R5.9 – R5.15, R5.17, R R5.1, R5.3, R5.5, R5.6, R5.9 –
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Decisions CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 5, Horstmann*
Decisions: Handling Infinite Cases CS 21a: Introduction to Computing I First Semester,
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
Fall 2006Slides adapted from Java Concepts companion slides1 Decisions Advanced Programming ICOM 4015 Lecture 5 Reading: Java Concepts Chapter 6.
If Statement if (amount
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Random Functions Selection Structure Comparison Operators Logical Operator
 Type Called bool  Bool has only two possible values: True and False.
Making Choices with if Statements
Chapter Goals To implement decisions using if statements
Chapter 4: Making Decisions.
Chapter 3 Edited by JJ Shepherd
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Chapter 5/6 Decisions.
JavaScript conditional
Topics The if Statement The if-else Statement Comparing Strings
Logical Operators & Truth Tables.
Data Types, Identifiers, and Expressions
Chapter 4: Decision Structures and Boolean Logic
VB Decisions, Conditions & If
Selection Statements.
Computer Science Core Concepts
Conditional Logic Presentation Name Course Name
Chapter 5 – Decisions Big Java by Cay Horstmann
Understanding Conditions
Chapter 6 - Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Truth tables Mrs. Palmer.
Introduction to Programming
Chapter 4: Decision Structures and Boolean Logic
Conditionals.
Presentation transcript:

Computer Science A 3: 10/2

Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program as tests or conditions that decide the programs behaviour. If-statement: if( logical expression ){ statements }

Multiple Alternatives Branch inside another branch if (condition1) { if (condition1a) statement1a; else statement1b; } else statement2;

Comparing Strings Don't use == for strings! if (input == "Y") // WRONG!!! Use equals method: if (input.equals("Y")) == tests identity, equals tests equal contents Case insensitive test ("Y" or "y") if (input.equalsIgnoreCase("Y")) s.compareTo(t) < 0 means: s comes before t in the dictionary "car"comes before "cargo" All uppercase letters come before lowercase: "Hello" comes before "car"

Lexicographic Comparison

Comparing Floating-Point Numbers To avoid roundoff errors, don't use == to compare floating-point numbers To compare floating-point numbers test whether they are close enough: |x - y| ≤ ε final double EPSILON = 1E-14; if (Math.abs(x - y) <= EPSILON) // x is approximately equal to y ε is a small number such as

Boolean operations && and || or ! not if (0 < amount && amount < 1000)... if (input.equals("S") || input.equals("M"))...

&& and || Operators

Boolean variables boolean married; Set to truth value: married = input.equals("M"); Use in conditions: if (married)... else... if (!married)... Also called flag It is considered bad style to write a test such as if (married == true)... // Don't Just use the simpler test if (married)...