Information Science 1

Similar documents
Information Science 1

Information Science 1

Information Science 1

Information Science 1

Lecture (04) Boolean Algebra and Logic Gates

Lecture (04) Boolean Algebra and Logic Gates By: Dr. Ahmed ElShafee

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 2: Using Data

8.3 Common Loop Patterns

Expressions & Assignment Statements

Programming in C++ 5. Integral data types

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

TA: Jade Cheng ICS 241 Recitation Lecture Notes #12 November 13, 2009

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CS February 17

Operators. Java operators are classified into three categories:

Operators & Expressions

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Programming for Engineers Iteration

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Information Science 1

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Chapter 7. Expressions and Assignment Statements

LECTURE 4. Logic Design

Flow Control. So Far: Writing simple statements that get executed one after another.

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

1.3b Type Conversion

UNIT- 3 Introduction to C++

Chapter 6 Primitive types

Fundamentals of Programming CS-110. Lecture 3

Computer Organization and Levels of Abstraction

Computer Organization and Levels of Abstraction

Boolean algebra. June 17, Howard Huang 1

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Cambridge International AS & A Level Computer Science

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Boolean Algebra & Digital Logic

Propositional Calculus. Math Foundations of Computer Science

COMP Primitive and Class Types. Yi Hong May 14, 2015

7/25/2016. Example: Addition of Unsigned Bit Patterns. ECE 120: Introduction to Computing. Adding Two Non-Negative Patterns Can Overflow

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Dr. Chuck Cartledge. 10 June 2015

LECTURE 17. Expressions and Assignment

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

LECTURE 3 C++ Basics Part 2

Introduction to Boolean Algebra

Introduction to Boolean logic and Logical Gates

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Additional Control Structures

REVIEW. The C++ Programming Language. CS 151 Review #2

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

JAVA OPERATORS GENERAL

Introduction to Boolean Algebra

Chapter 7. Expressions and Assignment Statements

Visual C# Instructor s Manual Table of Contents

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

ARG! Language Reference Manual

Operators And Expressions

More Programming Constructs -- Introduction

Logic Design: Part 2

Chapter 4: Basic C Operators

24 Nov Boolean Operations. Boolean Algebra. Boolean Functions and Expressions. Boolean Functions and Expressions

Expressions and Assignment Statements

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CSC Discrete Math I, Spring Sets

Full file at

2.2 Set Operations. Introduction DEFINITION 1. EXAMPLE 1 The union of the sets {1, 3, 5} and {1, 2, 3} is the set {1, 2, 3, 5}; that is, EXAMPLE 2

Programming in C++ 6. Floating point data types

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Unit 3. Operators. School of Science and Technology INTRODUCTION

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

COMP combinational logic 1 Jan. 18, 2016

Logical operators 20/09/2018. The unit pulse. Background

BOOLEAN ALGEBRA AND CIRCUITS

Arithmetic Operators. Portability: Printing Numbers

Values, Variables, Types & Arithmetic Expressions. Agenda

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Program Control Flow

Program Control Flow

SYNERGY INSTITUTE OF ENGINEERING & TECHNOLOGY,DHENKANAL LECTURE NOTES ON DIGITAL ELECTRONICS CIRCUIT(SUBJECT CODE:PCEC4202)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Programming Lecture 3

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Declaration and Memory

Propositional Calculus: Boolean Algebra and Simplification. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Operators in C. Staff Incharge: S.Sasirekha

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Introduction to Computer Architecture

C: How to Program. Week /Mar/05

Chapter 3. Boolean Algebra and Digital Logic

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Transcription:

Information Science Boolean Expressions Week College of Information Science and Engineering Ritsumeikan University

Topics covered l Terms and concepts from Week 9 l Binary (Boolean) logic History Boolean data type Relational and logical operators Boolean and algebraic expressions Operator precedence Truth tables and De Morgan s Law l Test 2

Recall Week 9 l White space l Line and block comment statements l Operator, Operand l (Arithmetic) expression l Integer division, Modulus l Mixed-type arithmetic l Implicit and explicit type casting l Shortcut operators l Operator precedence 3

Our goals today l To learn about Boolean Logic l To discuss relational and logical operators and how they are evaluated in expressions l To learn how to construct truth tables l To understand De Morgan s laws 4

The problem l Our daily life is based on many binary decision-making cognitive processes, which can often be represented as an algorithm with basic selection statements: Is condition true? No action A Yes action B l Many other decisions, however, require choosing from many alternatives 5

History: George Boole l Boolean logic is binary because it uses only two logical values: True ( or On or Yes ) False ( or Off or No ) l In 854, George Boole published his work An Investigation into the Laws of thought, where he described a system for symbolic and logical reasoning l This system was later called Boolean logic l Presently, Boolean logic is the basis for computer and program design 6

Boolean expressions and Boolean algebra l A Boolean expression has the form of a condition, <condition> l A Boolean expression always evaluates to either true or false l Boolean expressions are written using boolean-type operands and relational and logical operators l Boolean algebra provides the operations and the rules for working with Boolean variables and constants 7

Boolean variables and constants l Let B = {, }, where stands for (is equal to) FALSE and stands for TRUE. The variable x is called a Boolean variable if it assumes values only from B l In programs, we define Boolean variables (constants, fields, etc) the same way as other variables: boolean hasmiddlename; const boolean isflammable = TRUE; l The reserved (key-)words are: boolean (or bool), TRUE, and FALSE 8

Relational operators l Boolean expressions compute Boolean-type results and make use of the relational operators: < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to 9

Examples of Boolean (relational) expressions (-5 < 5) (4 <= -5) (3 > 3) (3 >= 3) (7 == ) (2!= 7) (5 = ) True False False True False True Error! Not a Boolean expression

Examples of assignments with Boolean expressions boolean b, b2, b3, b4, b5, b6; b = ( 4 < 5); // b is assigned TRUE b2 = ( 4 <= 3); // b2 is assigned FALSE b3 = ( 3 > 3); // b3 is assigned FALSE b4 = ( 4 < (3 + 2)); // b4 is assigned TRUE int x = 7; // and these are algebraic expressions b5 = ((x 5) <= 3); // b5 is assigned TRUE b6 = b5 + ; /* b6 is, in most programming languages, assigned TRUE */ l FALSE is usually associated with the value (zero), while TRUE with a non-zero value (e.g., -, 2, etc)

l Logical operators Besides relational operators, Boolean expressions also make use of the logical operators: The negation (logical NOT) is denoted by the symbol! or or. It is defined by!false == TRUE and!true == FALSE The Boolean sum (disjunction, logical OR) is denoted by or or and is defined by TRUE TRUE == TRUE, FALSE FALSE == FALSE, TRUE FALSE == TRUE, FALSE TRUE == TRUE The Boolean product (conjunction, logical AND) is denoted && or & or and is defined by TRUE && TRUE == TRUE, FALSE && FALSE == FALSE, TRUE && FALSE == FALSE, FALSE && TRUE == FALSE 2

3 AND and OR A B A && B FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE A B A B FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE

Boolean arithmetic l An easy way to evaluate Boolean (logical) expressions is to replace: TRUE with, and FALSE with, with +, && with *,! with NOT l Next step is then just to do the arithmetic: OR + + + AND * * * NOT() NOT() + * 4

5 Boolean operations and circuits x y x y z z l AND: z = x & y l OR: z = x y x z l NOT: z = ~x

6 Algebraic expressions: Operator precedence (postfix) ++, -- st (prefix) ++, --,!, NOT 2nd *, /, % 3rd +, - 4th <, >, <=, >= 5th ==,!= 6th &&, AND 7th, OR 8th (assign) =, +=, *=, etc. 9th

Truth tables B l A truth table can be used to show the relationships between the input and the output of an algorithm A C consider an alarm which sounds when: A. The lights are on and B. The door is open and C. The key is not in the ignition Alarm system P = A && B &&!C P Buzzzzz!!! Truth table: A B C P 7

Truth tables: Another x y z F(x, y, z) example l Problem: For the given truth table, construct the corresponding boolean expression l Solution: Since F(x, y, z) = if and only if (x == y == z == ) or (x == y ==, z == ) or (x ==, y == z == ), F(x, y, z) =!x &&!y &&!z!x &&!y && z x &&!y &&!z 8

9 De Morgan's Laws l Expressions with logical operators &&,, and! obey De Morgan's laws: l Example! (p && q) == (!p!q ) and! (p q) == (!p &&!q ) if (! ( x => - && x <= ) ) Now, let s make it simpler / easier to read: if ( x < - x > )

Other laws of Boolean logic (x ) == x and (x && ) == x Identity laws (x (!x)) == and (x && (!x)) == Domination laws ((x y) z) == (x (y z)) and Associative ((x && y) && z) == (x && (y && z)) laws (x y) == (y x) and Commutative (x && y) == (y && x) laws (x (y && z)) == ((x y) && (x z)) Distributive and laws (x && (y z)) == ((x && y) (x && z)) 2

2 Concepts you need to know after this class l Boolean logic and Boolean algebra l Boolean and algebraic expressions l Relational operators l Logical operators l Operator precedence l Truth tables l De Morgan's laws

22 l Read these slides again l Do the self-preparation assignments l Learn the vocabulary Homework l Consult, whenever necessary, the textbook materials

23 Next class l Fundamental Programming Constructs Basic syntax of high level languages Conditional & iterative control structures

Test 3 24