Types, Truth, and Expressions

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
If Statements & Relational Operators Programming.
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.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Chapter 2 Control.
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
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
IB Computer Science – Logic
If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Decision Making CMSC 201 Chang (rev ).
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
 Type Called bool  Bool has only two possible values: True and False.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
Chapter 3 Selection Statements
CPS120 Introduction to Computer Science
Selection (also known as Branching) Jumail Bin Taliba by
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
A mechanism for deciding whether an action should be taken
Lecture 3- Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
Boolean logic Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
JavaScript conditional
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Types, Identifiers, and Expressions
Bools and simple if statements
Types, Truth, and Expressions (Part 2)
Relational Operators Operator Meaning < Less than > Greater than
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Computer Science 210 Computer Organization
Selection Control Structure
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Expressions.
Logic of an if statement
Boolean logic Taken from notes by Dr. Neil Moore
Understanding Conditions
ECS15 boolean.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Computer Programming Basics
Relational and Logical Operators
Types, Truth, and Expressions
OPERATORS in C Programming
Relational and Logical Operators
OPERATORS in C Programming
Conditionals.
Types, Truth, and Expressions (Part 2)
Control 9 / 25 / 19.
Presentation transcript:

Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

What type(s) would we use To represent your: Weight Height Age Name Class status (freshman, sophomore, etc) Gender If you owe the university money If you are taking classes this semester

Yesterday’s Lab – Main Concept Review First we looked at the idea of Conditionals and the basic six operators: Less than: < Greater than: > Equal to: == (Not the same as =) Not equal to: != Less than or equal to: <= Greater than or equal to: >=

Yesterday’s Lab – Main Concept Review The results of a conditional operator is always a Boolean value. Meaning ??? Either True or False

Boolean Expressions George Boole’s (mid-1800’s) mathematics of logical expressions Boolean expressions (conditions) have a value of True or False Conditions are the basis of choices in a computer, and, hence, are the basis of the appearance of intelligence in them.

What is True, and What is False True: any nonzero number or nonempty object. 1, 100, “hello”, [a,b] False: zero number or empty object. 0, “”,[ ] Special values called “True” and “False”, which are just stand-ins for 1 and 0. However, they print nicely (True or False)

Boolean Expression Every boolean expression has the form: expression booleanOperator expression The result of evaluating something like the above is just True or False. Evaluating a boolean operation is a binary operation “if (a<b)” has two operands in expression However, remember what constitutes True or False in Python! “If (a)” will evaluate to True if a is non-zero!

Relational Operators Relational Operators have low preference 5 + 3 < 3 – 2 (5 + 3) < (3 - 2) 8 < 1 False

Selection Then we looked at how conditionals are applied in selection statements. Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing

Python if Statement if boolean_expression : suite evaluate the boolean_expression (True or False) if True, execute all statements in the suite

Warning About Indentation Elements of the “suite” must all be indented the same number of spaces/tabs Python only recognizes suites when they are indented the same “distance” You must be careful to get the indentation right to get suites right.

Python Selection, Round 2 if boolean expression: suite1 else: suite2 The process is: evaluate the boolean if True, run suite1 if False, run suite2

Chained Comparisons In python, chained comparisons work just like you would expect in a mathematical expression: Given myInt has the value 5 0 <= myInt <= 5 True 0 < myInt <= 5 > 10 False

Compound Expressions Logically 0 < X < 3 is actually (0 < X) and (X < 3) Logical Operators (lower case) and or not

Truth Tables

Truth Tables

Truth Tables

Truth Tables

Truth Tables

Compound Evaluation Logically 0 < X < 3 is actually (0 < X) and (X < 3) Evaluate using X with a value of 5: (0< X) and (X< 3) Parenthesis first: (True) and (False) Final value: False (Note: parentheses are not necessary in this case.)