“Operators” (i.e. “symbols”)

Slides:



Advertisements
Similar presentations
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Advertisements

CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
JavaScript, Third Edition
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
“Operators” (i.e. “symbols”) 1.Overview: Specific Symbols that Represent Specific Actions 2.Arithmetic 3.Relational 4.Boolean 5.Output values 1.
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.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
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.
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
ISBN Chapter 7 Expressions and Assignments Statements.
 Type Called bool  Bool has only two possible values: True and False.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Relational Operator and Operations
CMSC201 Computer Science I for Majors Lecture 03 – Operators
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Programming for Mechanical Engineers (ME 319)
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
Selections Java.
University of Central Florida COP 3330 Object Oriented Programming
Assignment statement:
Data Types, Identifiers, and Expressions
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.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Topics The if Statement The if-else Statement Comparing Strings
Data Types, Identifiers, and Expressions
And now for something completely different . . .
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Chapter 8 JavaScript: Control Statements, Part 2
Relational Operators Operator Meaning < Less than > Greater than
Summary Two basic concepts: variables and assignments Basic types:
MATLAB Logical Expressions
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
Boolean Expressions to Make Comparisons
LINEAR EQUATIONS.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Chapter 4: Expression and Operator
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Using C++ Arithmetic Operators and Control Structures
Operator and Expression
LINEAR EQUATIONS.
Operator King Saud University
DATA TYPES AND OPERATIONS
boolean Expressions Relational, Equality, and Logical Operators
Introduction to Python
Presentation transcript:

“Operators” (i.e. “symbols”) Overview: Specific Symbols that Represent Specific Actions Arithmetic Relational Boolean Output values

Overview: most Operators There are 3 primary groups of operators One programming operator is very different from its use in math: 1. ARITHMETIC + Addition - Subtraction * Multiplication /,\ Division ^ Exponentiation, i.e. “To the power of” 2. RELATIONAL < strictly less than > strictly greater than <= less than or equal to >= greater than or equal to == is equal to ~= is not equal to 3. BOOLEAN && “AND” || “OR” ~ “NOT” = “the assignment operator”

4 * 5 -5 Overview, cont. Operators work on operands. Binary Operator Requires two operands to work 4 * 5 operands Multiplication operator -5 Unary Operator Requires one operand to work operand Negative operator

kineticEnergy = 1 / 2 * mass * vel ^ 2 Overview, cont. There are 2 types of operands: Numerical 1, 3.5, -47 Logical true, false Arithmetic (+, -, /, *, ^, =) and relational (<, <=, >, >= ,==, ~=) operators work with numerical operands Numerical Operands kineticEnergy = 1 / 2 * mass * vel ^ 2 Arithmetic operators Assign operator: “place one or more values into memory”

Overview, cont. There are 2 types of operands: Numerical 1, 3.5, -47 Logical true, false Boolean (&&,||,~) operators work on logical operands “ if this is true and this is false… do something” if (it's raining outside) and (you have an umbrella) go, you won't get wet else stay inside! end

True, False, 1, and 0?! True False 1

3. Relational Operators Relational operators allow a comparison to be evaluated. Is thrust_a greater than thrust_b?  True / False Is surface1 equal to surface2?  True / False? Is load1 less than or equal to load2?  True / False? Examples: thrust_a > thrust_b Is thrust_a strictly greater than thrust_b? radius <=0 Is radius negative or zero? nb_attempts<= 3 Is the number of attempts less than or equal to 3? 3 >= nb_attempts Is 3 greater than or equal to the number of attempts? value ~= 2 Is value not equal to 2?

Relational Operators, cont. ***COMPARISON*** == y == 5 % “Does y hold the value 5?” % “Is y equal to 5?” Example: menuChosen == 1 % did user choose menu #1 ?

Relational Operators, cont. ***COMPARISON*** == y == 5 % “Does y hold the value 5?” % “Is y equal to 5?” Example: menuChosen == 1 % did user choose menu #1 ? Assignment = % A numerical operator y = 5; % “Store the value 5 in the % variable y” Note that == and = are DIFFERENT!

Spaces or not? When one relational operator is made up of 2 symbols (<=, >=, ~=, ==): KEEP THEM GLUED TOGETHER

Spaces or not? When one relational operator is made up of 2 symbols (<=, >=, ~=, ==): KEEP THEM GLUED TOGETHER Regardless of which operator is used, a space can be used before and/or after. All these are identical to MATLAB: thrustA<=thrustB %no spaces anywhere thrustA <=thrustB %1 space before the operator thrustA<= thrustB %1 space after the operator thrustA <= thrustB %1 space before AND after

4. Boolean Operators These operators take logical scalar values and perform some operation on them to yield a logical value Two Boolean operators allow to COMBINE relational expressions && Logical AND || Logical OR One Boolean operator allows to NEGATE the result ~ Logical NOT “Negates”: turns true values into false, and false values into true

Boolean Operator #1: && “logical and” Two & symbols (“Ampersand”), glued together && Both relational expressions must be true for the combined expression to be true X && Y yields true if and only if both X and Y are true e.g. (3 < 5) && (8 >= 8) ? (x < 1) && (x > 5) ? x = 52.1; (5.5 < x) && (x < 100.2) ?

&&, continued Use of parenthesis e.g. (3<5) && (8>=8) true same as 3<5 && 8>=8 true (x<3) && (x>5) false same as x<3 && x>5 false For sanity, at least use spaces before/after the operator!

True/False What is the result of the following statement? (2 > 3) && (3 < 29.3) True False Impossible to determine (22 > 3) && (3 > 29.3) (22 > x) && (x > 29.3) True False Impossible to determine (x<2) && (y>0)

True/False In other words, there are 4 options: F && T T && F F && F

Boolean Operator #2: || “logical or” Two | (“pipe”) symbols, glued together || At least ONE relational expressions must be true for the combined expression to be true X || Y yields true if either X or Y (or both) are true e.g. (3<5) || (5>=8) ? x = 4.2; (x< 3) || (x > 5) ?

True/False What is the result of the following statement? (2 > 3) || (3 < 29.3) True False Impossible to determine (22 > 3) || (3 > 29.3) (22 > x) || (x > 29.3) True False Impossible to determine (x<2) || (y>0)

True/False Again, there are 4 options: F || T T || F F || F T || T

Priorities between Boolean Operators Which operator has priority in the following? 1 + 1 + 0 * 1 Just like * has priority over + , && has priority over || What is the result of this statement? x = 44.5; y = 55; (x<=50) || (0<y) && (y<40) ? ((x<=50) || (0<y)) && (y<40) ? (x<=50) || ((0<y) && (y<40)) ?

Boolean Operator #3: NOT One ~ symbol (“tilde”) “NOT” : negates a value Example: x = true; %keyword is known to MATLAB y = ~x; %y now has the value false The value y entered by the user should NOT be between 4 and 9 cm, inclusive: % Suppose the user enters 7.4 as a value for y ~(4<=y && y<=9) ?

5. Operators: Result values Type Operand type Result type Arithmetic: Numbers Numbers e.g. 5 * 3 15 Relational: Numbers Logical e.g. 5 < 3 false Boolean: Logical Logical e.g. ~true false true & false

Order of Operations Operator Priority Parenthesis () Highest Exponentiation ^ Unary: -, NOT ~ Multiplication/division: * / \ Addition/subtraction: + - Relational: <, <=, >, >=, ==, ~= AND: && OR: || Assignment: = Lowest