Operators.

Slides:



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

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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
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.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Bell Ringer = – 5 = = ÷ -2 = =6. -7 – (-7) = After you have completed the bell ringer, take out your homework!
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CPS120: Introduction to Computer Science Operations Lecture 9.
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.
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.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
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.
Chapter Two Fundamental Programming Structures in Java Adapted from MIT-AITI slides Variables and Primitive Data Types 1.
1 CS161 Introduction to Computer Science Topic #6.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
LESSON 3 Expressions, Statements, & Operators. STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null.
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.
CompSci 230 S Programming Techniques
Relational Operator and Operations
CSE 220 – C Programming Expressions.
Lecture 3 Java Operators.
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Assignment statement:
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
ORDER OF OPERATIONS BEMDAS. 1. Brackets - ( ) or [ ]
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
C Operators, Operands, Expressions & Statements
Building Java Programs
Introduction to Programming – 4 Operators
Code Refresher Test #1 Topics:
Chapter 2: Java Fundamentals
Data Types and Expressions
SSEA Computer Science: Track A
OPERATORS AND EXPRESSIONS IN C++
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Operator King Saud University
5.03 Apply operators and Boolean expressions
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Operators

What are Operators? Operators are special symbols used for mathematical functions assignment statements logical comparisons Examples: 3 + 5 // uses + operator 14 + 5 – 4 * (5 – 3) // uses +, -, * operators Expressions can be combinations of variables, primitives and operators that result in a value

The Operator Groups There are 5 different groups of operators: Arithmetic operators Assignment operator Increment/Decrement operators Relational operators Conditional operators

Arithmetic Operators Java has 6 basic arithmetic operators - subtract + add - subtract * multiply / divide % modulo (remainder) ^ exponent (to the power of) Order of operations (or precedence) when evaluating an expression is the same as you learned in school (PEMDAS).

Order of Operations Example: 10 + 15 / 5; The result is different depending on whether the addition or division is performed first (10 + 15) / 5 = 5 10 + (15 / 5) = 13 Without parentheses, Java will choose the second case Note: you should be explicit and use parentheses to avoid confusion Should give more examples during lecture. Be dynamic teachers.

Integer Division In the previous example, we were lucky that (10 + 15) / 5 gives an exact integer answer (5). But what if we divide 63 by 35? Depending on the data types of the variables that store the numbers, we will get different results.

Integer Division Example int i = 63; int j = 35; System.out.println(i / j); Output: 1 double x = 63; double y = 35; System.out.println(x / y); Ouput: 1.8 The result of integer division is just the integer part of the quotient!

Assignment Operator The basic assignment operator (=) assigns the value of var to expr var = expr ; Java allows you to combine arithmetic and assignment operators into a single operator. Examples: x = x + 5; is equivalent to x += 5; y = y * 7; is equivalent to y *= 7;

Increment/Decrement Operators count = count + 1; can be written as: ++count; or count++; ++ is called the increment operator. count = count - 1; can be written as: --count; or count--; -- is called the decrement operator.

The increment/decrement operator has two forms: The prefix form ++count, --count first adds 1 to the variable and then continues to any other operator in the expression int numOranges = 5; int numApples = 10; int numFruit; numFruit = ++numOranges + numApples; numFruit has value 16 numOranges has value 6 The postfix form count++, count-- first evaluates the expression and then adds 1 to the variable int numOranges = 5; int numApples = 10; int numFruit; numFruit = numOranges++ + numApples; numFruit has value 15 numOranges has value 6

Relational (Comparison) Operators Relational operators compare two values Produces a boolean value (true or false) depending on the relationship operation is true when . . . a > b a is greater than b a >= b a is greater than or equal to b a == b a is equal to b a != b a is not equal to b a <= b a is less than or equal to b a < b a is less than b

Examples of Relational Operations int x = 3; int y = 5; boolean result; 1) result = (x > y); now result is assigned the value false because 3 is not greater than 5 2) result = (15 == x*y); now result is assigned the value true because the product of 3 and 5 equals 15 3) result = (x != x*y); x and y (15) is not equal to x (3)

Conditional Operators Symbol Name && AND || OR ! NOT Conditional operators can be referred to as boolean operators, because they are only used to combine expressions that have a value of true or false.

Truth Table for Conditional Operators x y x && y x || y !x True False

Examples of Conditional Operators boolean x = true; boolean y = false; boolean result; Let result = (x && y); now result is assigned the value false (see truth table!) Let result = ((x || y) && x); (x || y) evaluates to true (true && x) evaluates to true now result is assigned the value true

Using && and || Examples: (a && (b++ > 3)) (x || y) Java will evaluate these expressions from left to right and so will evaluate a before (b++ > 3) x before y Java performs short-circuit evaluation: it evaluates && and || expressions from left to right and once it finds the result, it stops.

Short-Circuit Evaluations (a && (b++ > 3)) What happens if a is false? Java will not evaluate the right-hand expression (b++ > 3) if the left-hand operator a is false, since the result is already determined in this case to be false. This means b will not be incremented! (x || y) What happens if x is true? Similarly, Java will not evaluate the right-hand operator y if the left-hand operator x is true, since the result is already determined in this case to be true.

POP QUIZ 1) What is the value of number? int number = 5 * 3 – 3 / 6 – 9 * 3; 2) What is the value of result? int x = 8; int y = 2; boolean result = (15 == x * y); 3) What is the value of result? boolean x = 7; boolean result = (x < 8) && (x > 4); 4) What is the value of numCars? int numBlueCars = 5; int numGreenCars = 10; int numCars = numGreenCars++ + numBlueCars + ++numGreeenCars; -12 false true 27

References Summary of Java operators Order of Operations (PEMDAS) http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html Order of Operations (PEMDAS) Parentheses Exponents Multiplication and Division from left to right Addition and Subtraction from left to right