What are Operators? Some useful operators to get you started.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
Data types and variables
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types CSE 115 Spring 2006 April 3 &
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Objectives You should be able to describe: Data Types
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CPS120: Introduction to Computer Science Operations Lecture 9.
Building Java Programs Primitive Data and Definite Loops.
CSC 107 – Programming For Science. The Week’s Goal.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Mathematical Calculations in Java Mrs. C. Furman.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
Operators.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
CompSci 230 S Programming Techniques
Expressions.
Lecture 3 Java Operators.
Expressions.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
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.
Debugging and Random Numbers
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Introduction to C++ October 2, 2017.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Examples of Primitive Values
Relational Operators Operator Meaning < Less than > Greater than
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Building Java Programs
Expressions and Assignment
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.
Data Types and Expressions
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Introduction to Python
Building Java Programs
Presentation transcript:

What are Operators? Some useful operators to get you started

What’s an Operator? Definition: An operator is a symbol that provides a shorthand way of telling your program to perform a computation or other type of action Unary operators: applied to a single variable or expression E.g., -y or j++ Binary operators: applied to two expressions E.g., x+y, x<y, x=y

Spreadsheet Example

Common Operators for Numbers Arithmetic +, -, *, / % gives remainder for integer division ++ (--) can be used to increment (decrement) values, and are unary operators Comparison == and != >, >=, <, <= Assignment = += (and -=, *=, /=) Negation (minus sign)

Numeric Examples

Assignment Issues Don’t mix up the assignment (=) and equality test (= =) operators Assignment moves RHS value into LHS variable LHS changes Equality test checks if LHS equals RHS Returns true if the are, false if not Neither side changes

Integer Division When integers are divided, the result is truncated 17/5  3 19/5  3 4/5  0 Remainders can be obtained with % (modulus) operator 17%5  2 19%5  4 4%5  4

Mixing Integers & Real Numbers Review: integers and real numbers have vastly different ranges of values Operations that mix integers and real numbers could exceed integer ranges Solution: Generally, when operators mix integers and real numbers, the values are “promoted” to real numbers before the operation takes place

Typecast C# will not let you assign values of one type to another type if it could cause problems—even if you “know” it is okay Typical example: assigning a double to an int int Test = 3.0; // will produce an error! Typecast operator: Unary operator puts the value type in parentheses in front of expression to eliminate warning int Test = (int)3.0; // Eliminates error Typecasts can also be used between compatible objects, as we’ll see later in the course

Typecast Example Notes: Only typecast when you are sure that you’ve got compatible types If they aren’t compatible (e.g., typecasting a negative number to an unsigned integer) the results will either be “unpredictable” or it will crash your program with an exception. Neither is good…

Common Operators for Strings Concatenation + “Hello” + “World” gives “HelloWorld” Comparison == and != These comparisons are case sensitive! Assignment = +=

string Operator Example Notes: Comparisons == and != test exact equality—they are case sensitive (like everything in C#) Because of case sensitivity, comparisons such as < and <= are not supported

Common Operators for Booleans Logical binary operators && : returns true if both sides are true || : returns true if either side is true Negation ! : Makes a true expression false or a false expression true Comparison == and != Conditional Boolean-expression ? Value1 : Value2 Assignment =

Conditional Operator Identical in usage to Excel =if( test, value-if-true, value-if-false) int X=17; int Y=12; string z=(X>Y) ? “X > Y” : “X <= Y”; int X=17; int Y=12; int z=(X>Y) ? X-Y : Y-X;

bool Operator Example

Parting words… Operators are generally intuitive once you start working with them Operators have “precedence” (e.g., * gets done before +) but use parentheses if there’s any doubt Remember that operators “return” a value Sometimes it’s the same type (e.g., x+y) Sometimes it’s different (e.g., x>y)