Thirteen conditional expressions: letting programs make “decisions”

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Logic Gates.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Six compound procedures and higher-order procedures.
Introduction to Computers and Programming Lecture 5 New York University.
Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Eight compound procedures and higher-order procedures.
Chapter 4: Control Structures: Selection
Six compound procedures and higher-order procedures.
Computer Science 101 Boolean Algebra. What’s next? A new type of algebra – Helps us A new type of algebra – Helps us With logical reasoningWith logical.
Digital Logic Circuits – Chapter 1 Section 1-3, 1-2.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Computer Programming Boolean Logic Trade & Industrial Education
Semantics of Predicate Calculus For the propositional calculus, an interpretation was simply an assignment of truth values to the proposition letters of.
31/01/ Selection If selection construct.
Expressions Methods if else Statements Loops Potpourri.
DIGITAL ELECTRONICS. Everything in digital world is based on binary system. Numerically it involves only two symbols 0 or 1. –0 = False = No –1 = True.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
Random Functions Selection Structure Comparison Operators Logical Operator
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Computer Programming Boolean Logic.
Operational Semantics of Scheme
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CPS120 Introduction to Computer Science
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Computer Science 210 Computer Organization
A basic tutorial for fundamental concepts
More important details More fun Part 3
Introduction to Computer Science / Procedural – 67130
Decisions Chapter 4.
Logic Gates.
EGR 2261 Unit 4 Control Structures I: Selection
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4: Decision Structures and Boolean Logic
Microsoft Visual Basic 2005 BASICS
Introduction To Robot Decision Making
Computers & Programming Languages
Types, Truth, and Expressions (Part 2)
Computer Science 210 Computer Organization
Chapter 4: Decision Structures and Boolean Logic
Logic Gates.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Logical Operations In Matlab.
Three Special Structures – Case, Do While, and Do Until
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Introduction to Programming
Computer Programming Boolean Logic Trade & Industrial Education
Chapter 4: Decision Structures and Boolean Logic
Validity and Soundness, Again
Conditionals.
Presentation transcript:

thirteen conditional expressions: letting programs make “decisions”

Recap: The Meta language Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define name value] Procedure calls [procedure args …] Procedure is run with the args as inputs Compound procedures [args … → exp] Makes a new procedure with the specified arguments and return value Expression for return value can refer to args Define and with [define name exp] [with name = exp … exp] Introduce new names or values for names

A new kind of expression: if [if test consequent alternative] If test is true, Then evaluate and return consequent Otherwise, evaluate and return alternative Some useful tests [= a b] Checks if a and b are the same [> a b], [≤ a b], etc. Compares numbers ► [define abs [n → [if [> n 0] n [- n]]]] ► [abs 5] 5 ► [abs -5] 5 ►

Some other useful tests [number? value], [string? value], [bitmap? value], [integer? value], [procedure? value] Tests what kind of data value is [odd? number], [even? number] Tests whether a number is odd or even Okay, maybe this isn’t so useful … [and test 1 test 2 … test n ] [or test 1 test 2 … test n ] [not test] Combines tests into more complicated tests

Boolean objects Everything in Meta is an expression, And all expressions have values, So then what kind of value is [= a b] ? Answer: a “truth value” – true or false These are named Booleans after George Boole, who invented Boolean Algebra, an early form of symbolic logic Meta has two magic data objects that are used to represent the answers to questions They’re named true and false.

Examples Suppose we say: [define a 7] [define b 8] [define c 9.5] What are the values of [> a b] [not [= b c]] [integer? c] [odd? a] [and [< 5 a] [< a 10]]

Executing a Boolean expression Evaluating tests is really just normal procedure execution [and [< 5 a] [< a 10]] First, execute [< 5 a] Call < with 5 and 7 as inputs < returns true Then call [< a 10] Call < with 7 and 10 as inputs < returns true Call and with true and true as arguments (actually, this part is a little more complicated, but that won’t matter until next quarter) And returns true

Predicates (question answerers) Procedures, like = or odd?, that return Booleans are called predicates They can be thought of as tests or question answerers [= 1 2] asks the question “are 1 and 2 the same?” [odd? 7] asks the question “is 7 an odd number?” And their return values can be thought of as the answers [= 1 2] returns false [odd? 7] returns true Predicates are an important type of procedure

User-defined predicates Predicates are just normal procedures That happen to return Booleans So you can (and will) write your own (sorry, you’ll see a less lame example soon) [define big-and-odd? [n → [and [> n 10000] [odd? n]]]] [big-and-odd? ] [and [big-and-odd? b] [< b ]]