CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Variables and Expressions Order of Operations Real Numbers and the Number Line Objective: To solve problems by using the order of operations.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
LESSON 6 – Arithmetic Operators
5.1 Algebraic Expressions. CAN YOU FOLLOW ORDER OF OPERATIONS?
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Chapter 2: Using Data.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
OPERATORS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
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.
APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
What are Operators? Some useful operators to get you started.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
Evaluate Is your answer 33 or 19? You can get 2 different answers depending on which operation you did first. We want everyone to get the same.
THE ORDER OF OPERATIONS 1-2. VOCABULARY… 1.Order of Operations – a. Work inside grouping symbols ( ) or [ ] b. Simplify any terms with exponents 3 2 c.
Question of the Day Solve for b: 2b + 7 = 15. Expressions and Equations Collecting Like Terms and Distributive Property.
CSC Programming for Science Lecture 5: Actual Programming.
Do Now: Evaluate
Expressions.
Lecture 3 Java Operators.
Nahla Abuel-ola / WIT.
Assignment and Arithmetic expressions
Simplifying Expressions; Order of Operations
Order of Operations ÷ + - X.
Please Excuse My Dear Aunt Sally
Arithmetic Operator Operation Example + addition x + y
Please Excuse My Dear Aunt Sally
43 Order of Operations  ( ) + - X.
Lecture 3 Expressions Richard Gesick.
With Assignment Operator
Objective The student will be able to:
Order of Operations.
Differences between Java and JavaScript
Order of Operations 1-2 Objective: Students will evaluate numerical expressions and algebraic expressions using the order of operations. S. Calahan 2008.
Chapter 3 Operators and Expressions
Data Types and Expressions
Objective The student will be able to: use the order of operations to evaluate expressions.
Chapter 4: Expression and Operator
I can USE THE ORDER OF OPERATIONS USE GROUPING SYMBOLS.
43 Order of Operations  ( ) + - X.
Operator King Saud University
Symbols and Expressions
5.03 Apply operators and Boolean expressions
Data Types and Expressions
43 Order of Operations  ( ) + - X.
Data Types and Expressions
Presentation transcript:

CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004

Some Things to Think About order of operations what gets calculated 1 st ? type casting changing data from one type of variable to a different type shortcut operators doing the same calculations with less typing

Order of Operations order in which calculations and other operations are performed by language / 2 // is result 3 or 4? operations that are done first have "higher precedence" pp , 188 "Please excuse my dear aunt Sally"

Order of Operations - 2 operations within parentheses are always done first inner before outer parentheses e.g., (5 * (2 + 3)) ("please") method calls next (lower precedence) Math.pow(2, 3) // Java's exponentiation // == 2*2*2 ("excuse")

Order of Operations - 3 following all have the same precedence: "unary" minus unary minus doesn't subtract from anything value = -5 ; value = - x; // reverses sign increment or decrement: ++ and -- type casting (changes data type) value = (double) 5 / 2 ;

Type Casting can change data type of a variable by putting a different data type in front in parentheses e.g., (int), (char), (float) can also put parentheses around an expression, and a cast before that, to change the data type of the result double a = (int) (3.0 / 2); // a? // how many conversions in above line?

Order of Operations - 4 then multiply ("my"), regular division ("dear"), and modulus operations *, /, % then addition ("aunt"), and subtraction ("Sally") +, -

Order of Operations - 5 then comparison operations (in order), >= // highest among these ==, != && (AND) || (OR) // lowest among these then assignment (equals) = e.g., height = 60; or shortcut assignment e.g., x += y; p. 188

Practice evaluate the following: x = 3 * / 2 – Math.min(3, 8) y = (9 + 2) == (7 + 4) z = (double) 3 / 2 ; //unifying type? a = (float) 5.0 / 2 ; //a decimal type b = (int) (7 / 2.0) ;

Shortcut Operators way to write code with less typing result is exactly same code may actually run faster examples ++a; // exactly same as a = a + 1; ++a; 4 keystrokes (including semicolon) a=a+1; 6 keystrokes a = a + 1; 10 keystrokes (4 spaces)

Addition and Subtraction ++ is the "increment operator" adds 1 -- is the "decrement operator" subtracts 1 both are "unary" operators, because they only work on 1 variable contra: a + b, c = d, e == f, etc.

Prefix versus Postfix prefix means put the operator before the variable e.g., ++a, -- b increments or decrements a variable before it is used in an expression a = 2; x = ++a; now x is 3, a is 3

Prefix versus Postfix - 2 postfix means put the operator after the variable e.g., a--, b++ decrements or increments a variable after it is used in an expression a = 2; x = a++; now x is 2, a is 3

Practice a=4; b=3; x=++a;// x? a? y=a -- ;// y? a? x= ++a + b -- ;// x? a? b? y= a -- - (++b);// y? a? b?

Other Shortcut Operators can add, subtract, multiply, divide, or get modulus not limited to add or subtract just 1 a += 2; // same as a = a + 2; a -= 2; // same as a = a - 2; a *= 2; // same as a = a * 2; a /= 2.0;// same as a = a / 2.0;

Other Shortcut Operators - 2 modulus = remainder after integer division ("clock arithmetic") use % symbol 5 % 2 == 1 if num % 2 == 0, num is an even number shortcut operator for modulus a %= 2; // same as a = a % 2;

Practice a=4; b=5; // a? a += 5; // a? b -= 4; // b? b *= 4; // b? a %= ++b; // a? b?

Shortcuts in Totals total = 0.00; sale = 3.00; total += sale; /* would work well in a loop: read a line from a file into sale variable, add it onto total, read another line into sale, etc. */