OPERATORS (1) CSC 111.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
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: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
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.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
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.
Mathematical Calculations in Java Mrs. G. Chapman.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mathematical Calculations in Java Mrs. C. Furman.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Building java programs, chapter 3 Parameters, Methods and Objects.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
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
CSE 220 – C Programming Expressions.
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Building Java Programs
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.
Arithmetic operations & assignment statement
Nahla Abuel-ola / WIT.
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
IDENTIFIERS CSC 111.
OPERATORS (2) CSC 111.
Chapter 2 - Introduction to C Programming
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Fundamentals 2.
Building Java Programs Chapter 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
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Review of Previous Lesson
Building Java Programs
Data Types and Expressions
Building Java Programs
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

OPERATORS (1) CSC 111

Outline 1. Types of Operators 2. Assignment Operator 3. Arithmetic Operators 4. Compound Operators

1. TYPES OF OPERATORS There are five types of operators Assignment Arithmetic Increment/Decrement Relational Logical

variable = expression; 2. ASSIGNMENT OPERATORS When a variable is declared, a memory space is allocated for it according to its type. However, it does NOT have a value yet. Assignment operators are used to “assign” (give) values to variables. SYNTAX variable = literal; Example 1 x = 10; // x is previously declared SYNTAX variable1 = variable2; x = 10; // x is previously declared y = 15; // y is previouslydeclared x = y; // x=15 y=15 Example 2 SYNTAX variable = expression; Example 3 x = 10; // x is previously declared y = 15; // y is previously declared x = x * y; // x=10*15 ➔ x=150

2. ASSIGNMENT OPERATORS PROGRAM What is the memory state of the following program: public class AssignmentOperator { public static void main (String[] args) // Declaration section: to declare needed variables int counter; // Input section: to enter values of used variables counter = 0; // Processing section: processing statements counter = counter + 1; // Output section: display program output System.out.println (“counter= “ + counter); } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14

2. ASSIGNMENT OPERATORS PROGRAM - SOLUTION int counter; 6 32 bits Undefined Value counter counter = 0; 8 32 bits counter counter = counter + 1; // update the value of counter 10 32 bits 1 counter UPDATED

PROGRAM – TRACING (Formal Solution) 2. ASSIGNMENT OPERATORS PROGRAM – TRACING (Formal Solution) NEW Final value of Program Line counter 1 to 5 Undeclared 6, 7 Undefined Value 8 9 10 1 public class AssignmentOperator { public static void main (String[] args) // Declaration section: to declare needed variables int counter; // Input section: to enter values of used variables counter = 0; // Processing section: processing statements counter = counter + 1; // Output section: display program output System.out.println (“counter= “ + counter); } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 11 to 14 1 Be sure that variables on the Right Hand Side already have values. Be sure that ALL used variables are already declared. Variables on both sides of the equation must be previously declared.

variable = operand1 operator operand2; 3. ARITHMETIC OPERATORS There are 5 arithmetic operators in Java Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) SYNTAX variable = operand1 operator operand2; Example 1 x = 10 + 5; // 10 and 5 are called operands Example 2 x = 10; y = x * 10; // x and 10 are the operands Example 3 x = 10; y = x * 10 + 5; // x and 10 are the operands of * // x*10 and 5 are the operands of +

The division of two integers truncate the decimal part of the result 3. ARITHMETIC OPERATORS DIVISION Example 1 int x = 15; int y = 2; int z = x / y; // z = 7 (the decimal part is truncated) Example 2 int x = 15; int y = 2; double z = x / y; // z = 7.0 (since x & y are integers) Example 3 int x = 15; double y = 2.0; double z = x / y; // z = 7.5 (since y is double) Example 4 double x = 15.0; double y = 2.0; double z = x / y; // z = 7.5 (since x & y are double) The division of two integers truncate the decimal part of the result

The operands of a mod operator must be of integer type 3. ARITHMETIC OPERATORS MODULUS Modulus is the remainder of the division of two numbers Example 1 x = 5 % 2; // x = 1 (1 < 2) Example 2 x = 24 % 2; // x = 0 (0 < 2) Example 3 x = 21 % 7; // x = 0 (0 < 7) Example 4 x = 8 % 3; // x = 2 (2 < 3) The result of a modulus (or mod) operation is always less than the divisor Any even number mod 2 is always zero (Ex. 2) Any odd number mod 2 is always 1 (Ex. 1) X mod Y, where X is multiple of Y, is always zero (Ex. 3) The mod operation is not commutative; i.e. X mod Y ǂ Y mod X The operands of a mod operator must be of integer type

This is equivalent to: (((3*7)-6)+((2*5)/4))+6 3. ARITHMETIC OPERATORS ORDER OF PRECEDENCE The order of precedence of the arithmetic operators is as follows: Parenthesis have the highest priority: they are evaluated from inside to outside. Multiplication, Division, and Modulus have the same priority rules. They are evaluated from left to right. Addition and Subtraction have the same priority rules. They are evaluated from left to right. 3 * 7 – 6 + 2 * 5 / 4 + 6 Example 1 No parenthesis, look for * / % 3 * 7 – 6 + 2 * 5 / 4 + 6 Step 1 Evaluate from left to right Step 2 21 – 6 + 10 / 4 + 6 This is an integer division Step 3 21 – 6 + 2 + 6 Evaluate from left to right Step 4 15 + 2 + 6 Evaluate from left to right Step 5 17 + 6 Evaluate from left to right Step 6 23 Final result This is equivalent to: (((3*7)-6)+((2*5)/4))+6

This is equivalent to: (3*(7-6))+((2*5)/(4+6)) 3. ARITHMETIC OPERATORS ORDER OF PRECEDENCE 3 *(7 – 6) + 2 * 5 / (4 + 6) Example 2 Evaluate parenthesis first Step 1 3 * 1 + 2 * 5 / 10 Look for * / % Step 2 3 * 1 + 2 * 5 / 10 Evaluate from left to right Step 3 3 + 10 / 10 Evaluate division Step 4 3 + 1 Evaluate addition Step 5 4 Final result This is equivalent to: (3*(7-6))+((2*5)/(4+6))

4. COMPOUND OPERATORS Java provides five compound operators: += (x += y equivalent to x = x + y) -= (x -= y equivalent to x = x - y) *= (x *= y equivalent to x = x * y) /= (x /= y equivalent to x = x / y) %= (x %= y equivalent to x = x % y) Example 1 int x = 10; int y = 5; x*= y; //x = x * y 32 bits 10 x 32 bits 5 y UPDATED 32 bits 50 x 32 bits 5 y Unchanged

4. COMPOUND OPERATORS int x = 10; int y = 5; Example 2 int x = 10; int y = 5; x*= y + 7; //x = x * (y + 7) 32 bits 10 x 32 bits 5 y UPDATED 32 bits 120 x 32 bits 5 y Unchanged

Self-Check Exercises (1) What is the memory state of the following program public class Accumulator { public static void main (String[] args) int a, sum; a = 10; sum = 0; sum = sum + a; System.out.println (“sum = “ + sum); } 1 2 3 4 5 6 7 8 9 10 11 Evaluate the following expressions: 23 + 7 % 2 – 3 15.0 + 3.0 * 2.0 / 5.0 30.0 % 6 + 1 W2.3 Operators1

Self-Check Exercises (2) Given x=5, y=7 and z=10. Evaluate the following expressions: y *= 2 * x + 5 – z; z %= x; Write a program that exchanges the values of two numbers. (This is known as swapping). For example, if x = 5 and y = 7; after swapping, they become x = 7 and y = 5. W2.3 Operators1