Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.

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

Types and Arithmetic Operators
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Objectives What are control structures Relational.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Computer Science Selection Structures.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
Mathematical Calculations in Java Mrs. C. Furman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Operators.
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 =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CompSci 230 S Programming Techniques
ARITHMETIC IN C Operators.
INSPIRING CREATIVE AND INNOVATIVE MINDS
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
2.5 Another Java Application: Adding Integers
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.
Multiple variables can be created in one declaration
Primitive and Reference Data Values
Operators and Expressions
OPERATORS (1) CSC 111.
Escape Sequences What if we wanted to print the quote character?
Primitive and Reference Data Values
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
Fundamentals 2.
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.
Chapter 2: Java Fundamentals
Data Types and Expressions
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Chapter 2: Java Fundamentals Operators

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment Operator Order of Precedence Increment/Decrement Operators Relational Operators Logical Operators

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 3 Operators Operators are special symbols used for: –mathematical functions –assignment statements –logical comparisons Examples of operators: –3 + 5 // uses + operator – – 4 * (5 – 3) // uses +, -, * operators Expressions: can be combinations of variables and operators that result in a value

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 4 Groups of Operators There are 5 different groups of operators: –Arithmetic Operators –Assignment Operator –Increment / Decrement Operators –Relational Operators –Logical Operators

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 5 Java Arithmetic Operators Addition+ Subtraction– Multiplication  Division/ Remainder (modulus )%

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 6 Arithmetic Operators The following table summarizes the arithmetic operators available in Java. This is an integer division where the fractional part is truncated.

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 7 Example of division issues : 10 / 3 gives / 3 gives As we can see, if we divide two integers we get an integer result. if one or both operands is a floating-point value we get a floating-point result. Example

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 8  Generates the remainder when you divide two integer values. 5%3 gives 25%4 gives 1 5%5 gives 05%10 gives 5  Modulus operator is most commonly used with integer operands. If we attempt to use the modulus operator on floating-point values we will garbage! Modulus

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 9 Order of Precedence ( ) evaluated first, inside-out , /, or % evaluated second, left-to-right +,  evaluated last, left-to-right

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 10 Basic Assignment Operator We assign a value to a variable using the basic assignment operator (=). Assignment operator stores a value in memory. The syntax is leftSide = rightSide ; Examples: i = 1; start = i; sum = firstNumber + secondNumber; avg = (one + two + three) / 3; Allways it is a variable identifier. It is either a literal | a variable identifier | an expression.

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 11 The Right Side of the Assignment Operator The Java assignment operator assigns the value on the right side of the operator to the variable appearing on the left side of the operator. The right side may be either: Literal: ex. i = 1; Variable identifier: ex. start = i; Expression: ex. sum = first + second;

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 12 Assigning Literals In this case, the literal is stored in the space memory allocated for the variable at the left side. int firstNumber=1, secondNumber; firstNumber = 234; secondNumber = 87; A A B B firstNumber 1 1 secondNumber ??? A. A. Variables are allocated in memory. B. B. Literals are assigned to variables. firstNumber 234 secondNumber 87 Code State of Memory

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 13 Assigning Variables In this case, the value of the variable at the right side is stored in the space memory allocated for the variable at the left side. int firstNumber=1, i; firstNumber = 234; i = firstNumber; A A B B firstNumber 1 1 i ??? A. A. Variables are allocated in memory. B. B. values are assigned to variables. firstNumber 234 i Code State of Memory

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 14 Assigning Expressions In this case, the result of the evaluation of the expression is stored in the space memory allocated for variable at the left side. int first, second, sum; first = 234; second = 87; Sum = first + second A A B B A. A. Variables are allocated in memory. B. B. Values are assigned to variables. Code State of Memory first 1 1 second ??? sum ??? first 234 second 87 sum 321

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 15 Updating Data Code State of Memory number ??? A. A. The variable is allocated in memory. B. 237 number B. The value 237 is assigned to number. int number; number = 237; A A B B C C number = 35; C C. The value 35 overwrites the previous value 237. number 237 number 35

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 16 public class Sum { // main method public static void main( String args[] ){ int a, b, sum; a = 20; b = 10; sum = a + b; System.out.println(a + ” + ” + b + “ = “ + sum); } // end main } // end class Sum Example: Sum of two integer

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 17 Arithmetic/Assignment Operators Java allows combining arithmetic and assignment operators into a single operator: Addition/assignment+= Subtraction/assignment  = Multiplication/assignment  = Division/assignment/= Remainder/assignment %=

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 18 The syntax is leftSide Op= rightSide ; This is equivalent to: leftSide = leftSide Op rightSide ; x%=5;  x = x % 5; x*=y+w*z;  x = x*(y+w*z); Arithmetic/Assignment Operators Allways it is a variable identifier. It is an arithmetic operator. It is either a literal | a variable identifier | an expression.

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 19 Increment/Decrement Operators Only use ++ or   when a variable is being incremented/decremented as a statement by itself. x++; is equivalent to x = x+1; x--; is equivalent to x = x-1;

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 20 Relational Operators Relational operators compare two values They Produce a boolean value (true or false) depending on the relationship OperationIs true when a >ba is greater than b a >=ba is greater than or equal to b a ==ba is equal to b a !=ba is not equal to b a <=ba is less than or equal to b a <ba is less than b

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 21 Example int x = 3; int y = 5; boolean result; result = (x > y); now result is assigned the value false because 3 is not greater than 5

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 22 Logical Operators Symbol Name && AND || OR ! NOT || TF TTT FTF && TF TTF FFF

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 23 Example boolean x = true; boolean y = false; boolean result; result = (x && y); result is assigned the value false result = ((x || y) && x); (x || y) evaluates to true (true && x) evaluates to true result is then assigned the value true

Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 24 Operators Precedence Parentheses(), inside-out Increment/decrement++, --, from left to right Multiplicative*, /, %, from left to right Additive+, -, from left to right Relational, =, from left to right Equality==, !=, from left to right Logical AND&& Logical OR|| Assignment=, +=, -=, *=, /=, %=