LESSON 6 – Arithmetic Operators

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

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
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 Chapter 2: Elementary Programming Shahriar Hossain.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 4: Basic C Operators
Expressions, Data Conversion, and Input
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Recap……Last Time [Variables, Data Types and Constants]
Operators & Expressions
Doing math In java.
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.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Expressions.
Lecture 3 Java Operators.
Lecture 3: Operators, Expressions and Type Conversion
Relational Operations
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
ITM 352 Expressions, Precedence, Working with Strings Class #5
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Numerical Data Types.
With Assignment Operator
Expressions and Assignment
Assignment Operators Topics Increment and Decrement Operators
Chapter 3 Operators and Expressions
Data Types and Expressions
Primitive Types and Expressions
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

LESSON 6 – Arithmetic Operators JAVA PROGRAMMING LESSON 6 – Arithmetic Operators

Arithmetic operators and expressions As in most languages, expressions can be formed in Java using variables, constants, and arithmetic operators. Java has five (5) arithmetic operators. Operator Meaning Type Example + Addition Binary total = cost + tax; - Subtraction Binary cost = total – tax; * Multiplication Binary tax = cost * rate; / Division Binary salePrice = original / 2; % Modulus Binary remainder = value % 5;

Shorthand assignment statement Shorthand assignment notation combines the assignment operator (=) and an arithmetic assignment operator It is used to change the value of a variable by adding, subtracting, multiplying, or dividing by a specified value The general form is Variable Op = Expression which is equivalent to Variable = Variable Op (Expression) The Expression can be another variable, a constant, or a more complicated expression Op can be are +, -, *, /, or % Example: a += 4; Equivalent to: a = a + 4;

Shorthand assignment statement Example: Equivalent To: count += 2; count = count + 2; sum -= discount; sum = sum – discount; bonus *= 2; bonus = bonus * 2; time /= rushFactor; time = time / rushFactor; change %= 100; change = change % 100; amount *=count1+count2 amount = amount * (count1 + count2);

Arithmetic Operators and Expressions Promotion If an arithmetic operator is combined with int operands, then the resulting type is int If an arithmetic operator is combined with one or two double operands, then the resulting type is double If different types are combined in an expression, then the resulting type is the right‐most type on the following list that is found within the expression byte→short→int→long→float→double char Exception: If the type produced should be byte or short (according to the rules above), then the type produced will actually be an int

Arithmetic Operators and Expressions An expression can be fully parenthesized in order to specify exactly what subexpressions are combined with each operator If some or all of the parentheses in an expression are omitted, Java will follow precedence rules to determine, in effect, where to place them However, it's best (and sometimes necessary) to include them

Precedence rule Parentheses rule: Evaluate expressions in parentheses separately. Evaluate nested parentheses from the inside out. Operator precedence rule: Operators in the same expression are evaluated in the order determined by their precedence (from the highest to the lowest). Operator Precedence method call highest precedence - (unary) new, type cast *, /, % +, - (binary) = Lowest precedence Left associative rule: Operators in the same expression and at the same precedence level are evaluated in left-to-right order.

Precedence rule When two operations have equal precedence, the order of operations is determined by associativity rules Unary operators of equal precedence are grouped right‐toleft +‐+rate is evaluated as +(‐(+rate)) Binary operators of equal precedence are grouped left‐toright base + rate + hours is evaluated as (base + rate) + hours Exception: A string of assignment operators is grouped right‐to‐left n1 = n2 = n3; is evaluated as n1 = ( n2 = n3);

Precedence rule Evaluation of z – (a + b / 2) * w / y z ‐ (a + b / 2) * w / y Operator, reason evaluated ‐‐/‐‐‐ /, parens and precedence ‐‐‐+‐‐‐‐‐‐‐ +, parens ‐‐‐‐‐‐‐‐‐‐‐‐‐*‐‐ *, precendence, left assoc. ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐/‐‐ /, precedence

Increment and decrement operator Operator Name Description ++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++ postincrement The expression (var++) evaluates to the original value in var and increments var by 1. ‐‐var predecrement The expression (‐‐var) decrements var by 1 and evaluates to the new value in var after the decrement. var‐‐ postdecrement The expression (var‐‐) evaluates to the original value var and 10 in decrements var by 1.

Increment and decrement operator If n is equal to 2, then 2*(++n) evaluates to 6 If n is equal to 2, then 2*(n++) evaluates to 4 Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i.

JAVA PROGRAMMING THE END