CS 106 Logic and Conditionals (Boolean Expressions)

Slides:



Advertisements
Similar presentations
TRUTH TABLES Section 1.3.
Advertisements

TRUTH TABLES The general truth tables for each of the connectives tell you the value of any possible statement for each of the connectives. Negation.
Logic & Critical Reasoning
Chapter 3 section 2. Please form your groups The 1 st column represents all possibilities for the statement that can be either True or False. The 2 nd.
1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.
1. Propositions A proposition is a declarative sentence that is either true or false. Examples of propositions: The Moon is made of green cheese. Trenton.
Logic ChAPTER 3 1. Truth Tables and Validity of Arguments
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
IB Math Studies – Topic 3. IB Course Guide Description.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Boolean Algebra Computer Science AND (today is Monday) AND (it is raining) (today is Monday) AND (it is not raining) (today is Friday) AND (it is.
Introduction Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Objectives What are control structures Relational.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Truth Tables for Negation, Conjunction, and Disjunction.
3.2 – Truth Tables and Equivalent Statements
TRUTH TABLES. Introduction Statements have truth values They are either true or false but not both Statements may be simple or compound Compound statements.
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 3.2 Truth Tables for Negation, Conjunction, and Disjunction.
Chapter 1 Section 1.4 More on Conditionals. There are three statements that are related to a conditional statement. They are called the converse, inverse.
Section 1-4 Logic Katelyn Donovan MAT 202 Dr. Marinas January 19, 2006.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
BUILDING COMPUTER CIRCUITS prepared by Burak Galip ASLAN September, 2006 BOOLEAN LOGIC AND GATES CONTROL CIRCUITS.
Chapter 1 The Logic of Compound Statements. Section 1.1 Logical Form and Logical Equivalence.
Data types and their representation Jordi Cortadella Department of Computer Science.
BY: MISS FARAH ADIBAH ADNAN IMK. CHAPTER OUTLINE: PART III 1.3 ELEMENTARY LOGIC INTRODUCTION PROPOSITION COMPOUND STATEMENTS LOGICAL.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
Math 240: Transition to Advanced Math Deductive reasoning: logic is used to draw conclusions based on statements accepted as true. Thus conclusions are.
Mt263f-11 Discrete Structures Li Tak Sing( 李德成 ) Room A
Fall 2002CMSC Discrete Structures1 Let’s get started with... Logic !
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Logical Form and Logical Equivalence Lecture 2 Section 1.1 Fri, Jan 19, 2007.
Logic Disjunction A disjunction is a compound statement formed by combining two simple sentences using the word “OR”. A disjunction is true when at.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
3.3: Truth Tables. Types of Statements Negation: ~p Conjunction: p ˄ q (p and q) Disjunction: p V q (p or q, or both) Conditional: p → q (if p, then q)
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Boolean Algebra. Logical Statements A proposition that may or may not be true:  Today is Monday  Today is Sunday  It is raining.
Boolean Logic. Boolean Operators (T/F) xyx AND y FFF FTF TFF TTT xyx OR y FFF FTT TFT TTT xNOT x FT TF.
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
© 2010 Pearson Prentice Hall. All rights reserved. CHAPTER 3 Logic.
Warmup Answer the following True/False questions in your head: I have brown hair AND I am wearing glasses I am male OR I am wearing sneakers I am NOT male.
How do I show that two compound propositions are logically equivalent?
Logical Form and Logical Equivalence Lecture 1 Section 1.1 Wed, Jan 12, 2005.
Boolean Algebra Monday/Wednesday 7th Week. Logical Statements Today is Friday AND it is sunny. Today is Friday AND it is rainy. Today is Monday OR it.
Introductory Logic PHI 120 Presentation: "Truth Tables – Validity vs. Soundness" This PowerPoint Presentation contains a large number of slides, a good.
1 Propositional Logic Introduction. 2 What is propositional logic? Propositional Logic is concerned with propositions and their interrelationships. 
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Boolean Logic.
TRUTH TABLES. Introduction The truth value of a statement is the classification as true or false which denoted by T or F. A truth table is a listing of.
Notes - Truth Tables fun, fun, and more fun!!!!. A compound statement is created by combining two or more statements, p and q.
Logic and Boolean Algebra. Logic statements  Real life statements  “I use public transportation when the car is broken and I have to go to work”  “All.
CEC 220 Digital Circuit Design Boolean Algebra I Wed, Sept 2 CEC 220 Digital Circuit Design Slide 1 of 13.
CEC 220 Digital Circuit Design Boolean Algebra Friday, January 17 CEC 220 Digital Circuit Design Slide 1 of 22.
Fundamental Logic Gates And, Or, Not. Logic Gates: The Basics Regulate the flow of electricity within circuits to perform desired functionalities Each.
Spring 2003CMSC Discrete Structures1 Let’s get started with... Logic !
3/6/20161 Let’s get started with... Logic !. 3/6/20162 Logic Crucial for mathematical reasoningCrucial for mathematical reasoning Used for designing electronic.
Conditional statement or implication IF p then q is denoted p ⇒ q p is the antecedent or hypothesis q is the consequent or conclusion ⇒ means IF…THEN.
Assign Yourself and Do Now Thursday, January 10, 2013.
CS 115 Lecture 9 Augmented assignment; Boolean logic; random numbers Taken from notes by Dr. Neil Moore.
Aspatial Selection. Table Queries Table queries are a very common GIS aspatial operation Selects a subset of records based on values of specific attributes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Logical Operators (Connectives) We will examine the following logical operators: Negation (NOT,  ) Negation (NOT,  ) Conjunction (AND,  ) Conjunction.
Relational Operator and Operations
A mechanism for deciding whether an action should be taken
Boolean logic Taken from notes by Dr. Neil Moore
Boolean logic Taken from notes by Dr. Neil Moore
Algebra: Variables and Expressions
Presentation transcript:

CS 106 Logic and Conditionals (Boolean Expressions)

Enlarging our Repertoire So far we know how to write VB statements that change the values of variables, including properties of objects Our next type of statement is a conditional: it allows us to change what the program does based on the outcome of a test 2

Tests and Choices To allow a program to make choices based on conditions we need to introduce a new programming construct But first we look at how we program the tests that will determine which branch our program takes 3

Expressions An expression in VBA is a chunk of code that evaluates to a value Arithmetic expressions include simple numbers and expressions built with arithmetic operators and functions, such as 44 varA + 12 String expressions include simple strings and expressions built with the string operator & and possibly string functions “Hello, I’m a string.” “Your cost is “ & FormatCurrency(iceCreamTotal) 4

The Boolean Data Type (named for George Boole) A Boolean variable is one that has two possible values: True and False A Boolean expression is an expression that can evaluate to True or False The constants True and False are the simplest Boolean expressions More interesting examples are created by, for example, comparing two things Complex Boolean expressions are built up out of simple ones using Boolean operations 5

Relational Operators Relational Operators are used to create simple Boolean expressions They include: =equal? not equal? <less than? <=less than or equal? >greater than? >= greater than or equal? 6

Examples of True/False Expressions Dim varA, varB, varC As Double varA = 1 varB = 2 varC = 2 varA < varB is true varA <= varB is true varC < varB is false varC <= varB is true varB = varC is true‘note dual role of the symbol = 7

Logical Operators More complex Boolean expressions are built using Boolean operators. The most common Boolean operators are And, Or, and Not Other Boolean operators, such as exclusive or, are available if needed Not reverses the truth of its argument: Not (True) = False and vice-versa 8

And And is true if and only if both its arguments are true This is called a truth table. The letters X and Y represent Boolean expressions. We have to look at all possible combinations of T and F for X and Y XYX And Y TTT TFF FTF FFF 9

Or Or is true if and only if at least one of its arguments is true. This differs from the usual meaning of “or” (this or is more like and/or) XYX Or Y TTT TFT FTT FFF 10

More Complex Expressions These can be worked out using truth tables Consider (Not X) Or (X and Y) XYNot XX And YNot X Or (X And Y) TTFTT TFFFF FTTFT FFTFT 11

Another Example Show Not (A And B) is logically equivalent to (Not A) Or (Not B) ABA And BNot (A And B) TTTF TFFT FTFT FFFT ABNot ANot B(Not A) Or (Not B) TTFFF TFFTT FTTFT FFTTT 12

A Couple of Pointers Some math/relational expressions do not translate directly. For example, A < B <= C would be written as (A < B) And (B <= C) Be careful with And and Or. Normally both parts of an And expression are evaluated even if the first one is false, and both parts of an Or expression are evaluated even if the first one is true. Standard example: (X = 0) Or (Y/X <5) (could cause runtime error if X = 0) 13

Other Uses for Logic Logical expressions are a part of database queries. If you will be using databases and writing queries, understanding how to form logical expressions will be very useful. You can use logic to form advanced queries in Google We’ll limit ourselves to three operators: And, Or, and Not. They are sufficient to write any logical expression, though not always in the simplest way. If you want to use others, feel free, but make sure you understand their truth tables. 14