Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

If Statements & Relational Operators Programming.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Visual C++ Programming: Concepts and Projects
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Absolute Value Equalities and Inequalities Absolute value: The distance from zero on the number line. Example: The absolute value of 7, written as |7|,
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
A Level Computing#BristolMet Session Objectives U2#S9 MUST identify operators and operands SHOULD describe different types of operator COULD Create an.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
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.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Chapter 51 Logical Operators Used with Boolean expressions Not – makes a False expression True and vice versa And – will yield a True if and only if both.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Chapter 2 Inequalities. Lesson 2-1 Graphing and Writing Inequalities INEQUALITY – a statement that two quantities are not equal. SOLUTION OF AN INEQUALITY.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Lesson 1.4 Equations and Inequalities Goal: To learn how to solve equations and check solutions of equations and inequalities.
Doing math In java.
Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 4: Understand and implement Decisions.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Rational Expressions relational operators logical operators order of precedence.
 Type Called bool  Bool has only two possible values: True and False.
Relational Operator and Operations
Operators and Expressions
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Bool operators and, or, not
Computing Fundamentals
More important details More fun Part 3
Relational Operations
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Logical Operators & Truth Tables.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Bools and simple if statements
Relational Operators Operator Meaning < Less than > Greater than
All the Operators 4-Dec-18.
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Expressions.
Chapter 2: Java Fundamentals
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Lecture 9: Implementing Complex Logic
Introduction to Python
Presentation transcript:

Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1

Relational Operators Conditional expressions of form operand1 relationalOperator operand2 – Comparing operand1 and operand2 in some way Python relational operators: >greater than <less than <= less than or equal to >=greater than or equal to != is not equal to ==is equal to (not same as =)

Relational Operators Example

Relational and Arithmetic Operators Can combine in same condition x = 3 y = 7 if x * 2 < y + 1: … Precedence: Arithmetic operators done first 1.Evaluate expressions on either side of relational operator to get 2 values 2.Evaluate relational operator in term of those 2 values to get either True or False

Relational Operators and Types Some operators only make sense for certain types – Numbers: > = <= == != Be careful with floats and == – Strings: == != password = input(“Enter password:”) if password == “xyzzy”: … – Can do > = <= but get strange results Can only compare similar types “Fred” > 2  error

Equality and Float Type == checks whether operands are same number – Can be problem with floats due to lack of precision x = 5 * 2/3 y = 2/3 * 5 if x == y: …  False

Equality and Float Type Goal: determine whether x and y are “close enough” – Within some “margin of error” of each other Method: – Compute difference between the values – Take absolute value Difference may be positive or negative Have built-in fabs function – Compare with “margin of error” using < instead of == x = 5 * 2/3 y = 2/3 * 5 if math.fabs(x – y) < : …

Boolean Logic Some problems involve multiple conditions – “x must be between 1 and 10” – “either a or b must not be 0” Can create using and, or, other boolean operators – x must be greater than or equal to 1 and less than or equal to 10 – a must not be 0 or b must not be 0

Boolean Expressions Syntax: conditionalExpr booleanOp conditionalExpr Examples: – if x >= 1 and x <= 10: … – if a != 0 or b != 0: …

Boolean Operators and: true if both operands are true – true and true  true – true and false  false – false and true  false – false and false  false or: true if either operands are true – true or true  true – true or false  true – false or true  true – false or false  false

not Operator not: Reverses boolean value of unary operand – Syntax: not conditionalExpr not true  false not false  true – Example: if not x > 0: … Can often just use “opposite” relational operator – if x <= 0: … Commonly used to reverse value of boolean function – if not math.isfinite(x): …

Precedence and Boolean Operators Precedence: 1.Evaluate arithmetic expressions in conditional expressions 2.Evaluate conditional expressions to True or False 3.Evaluate boolean operators using those values 1. not evaluated first 2. and evaluated next 3. or evaluated last – Can always use () if not sure!