OPERATORS (2) CSC 111.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
Chapter 4: Basic C Operators
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
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.
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:
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Operators.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
CompSci 230 S Programming Techniques
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
University of Central Florida COP 3330 Object Oriented Programming
Building Java Programs
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
OPERATORS (1) CSC 111.
Chapter 2: Basic Elements of Java
Lecture Notes – Week 3 Lecture-2
Building Java Programs
Fundamentals 2.
Building Java Programs
Expressions and Assignment Statements
Expressions and Assignment
Building Java Programs
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.
Expressions.
Chapter 2: Java Fundamentals
Chapter 2 Programming Basics.
Data Types and Expressions
Building Java Programs
Chap 7. Advanced Control Statements in Java
Review of Previous Lesson
Building Java Programs
Problem 1 Given n, calculate 2n
Presentation transcript:

OPERATORS (2) CSC 111

Outline 1. Types of Operators 2. Increment/Decrement Operators 3. Relational Operators 4. Logical Operators 5. Type casting

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

2. INCREMENT/DECREMENT Two increment operators are used Pre-increment ++x increment then act Post-increment x++ act then increment Two decrement operators are used Pre-decrement --x decrement then act Post-decrement x-- act then decrement

PRE-INCREMENT (INCREMENT THEN ACT) 2. INCREMENT OPERATOR PRE-INCREMENT (INCREMENT THEN ACT) SYNTAX ++variable; Example 1 counter = 0; // counter is previously declared ++counter; // counter = counter + 1 // counter should previously have a value After this example, counter = 1 Example 2 x = 3; // x is previously declared y = ++x; // y is previously declared // x already has a value In Example 2, (y = ++x) is equivalent to the following statements in this order: x = x + 1; //increment y = x; //act (assign) 1 2 After this example, x = 4 and y = 4

PRE-INCREMENT (INCREMENT THEN ACT) 2. INCREMENT OPERATOR PRE-INCREMENT (INCREMENT THEN ACT) Example 3 a = 7; // a is previously declared System.out.println (++a); //increment then act (print) In Example 3, System.out.println (++a) is equivalent to the following two statements in this order: a = a + 1; //increment System.out.println (a); //act (print) 1 2 The program output is 8 Example 4 a = 5; // a is previously declared b = ++a % 2; // b is previously declared // a has already a value In Example 4, b = ++a % 2 is equivalent to the following two statements in this order: a = a + 1; //increment b = a % 2; //act (mod) 1 2 After this example, a = 6 and b = 0

POST-INCREMENT (ACT THEN INCREMENT) 2. INCREMENT OPERATOR POST-INCREMENT (ACT THEN INCREMENT) SYNTAX variable++; Example 1 counter = 0; // counter is previously declared counter++; // counter = counter + 1 // counter should previously have a value After this example, counter = 1 Example 2 x = 3; // x is previously declared y = x++; // y is previously declared // x already has a value In Example 2, (y = x++) is equivalent to the following statements in this order: y = x; //act (assign) x = x + 1; //increment 1 2 After this example, x = 4 and y = 3

POST-INCREMENT (ACT THEN INCREMENT) 2. INCREMENT OPERATOR POST-INCREMENT (ACT THEN INCREMENT) Example 3 a = 7; // a is previously declared System.out.println (a++); //act (print) then increment In Example 3, System.out.println (a++) is equivalent to the following two statements in this order: System.out.println (a); //act (print) a = a + 1; 1 2 The program output is 7 Example 4 a = 5; // a is previously declared b = a++ % 2; // b is previously declared // a already has a value In Example 4, b = a++ % 2 is equivalent to the following two statements in this order: b = a % 2; //act (mod) a = a + 1; //increment 1 2 After this example, a = 6 and b = 1

2. INCREMENT OPERATOR NOTES When a variable is used by itself, there is no difference between the post-increment and the pre-increment: counter++; ++counter; The following statements have all the same effect: counter = counter + 1; counter++; ++counter; counter +=1;

PRE-DECREMENT (DECREMENT THEN ACT) 2. DECREMENT OPERATOR PRE-DECREMENT (DECREMENT THEN ACT) The same rules of the increment operator. However, it decrements rather than increments. SYNTAX --variable; Example 1 x = 3; // x is previously declared y = --x; // y is previously declared // x already has a value In Example 2, (y = --x) is equivalent to the following statements in this order: x = x - 1; //decrement y = x; //act (assign) 1 2 After this example, x = 2 and y = 2

POST-DECREMENT (ACT THEN DECREMENT) 2. DECREMENT OPERATOR POST-DECREMENT (ACT THEN DECREMENT) SYNTAX variable--; Example 1 x = 3; // x is previously declared y = x--; // y is previously declared // x already has a value In Example 2, (y = x--) is equivalent to the following statements in this order: y = x; //act (assign) x = x -1; //decrement 1 2 After this example, x = 2 and y = 3

2. DECREMENT OPERATOR NOTES When a variable is used by itself, there is no difference between the post-increment and the pre-increment: counter--; --counter; The following statements have all the same effect: counter = counter – 1; counter--; --counter; counter -=1;

3. RELATIONAL OPERATORS Relational operators are used to compare items. Java uses the following relational operators: == equal to != not equal to < less than <= less than or equal > greater than >= greater than or equal Logical expressions use relational operators. Logical expressions evaluate to either true or false. The result of a logical expression is stored in a variable of type boolean. int x = 10, y = 15, z = 10; char ch1 = ‘a’, ch2 = ‘z’; //Unicode(‘a’) = 97, Unicode(‘z’) = 122 boolean result; result = (x < y); //result = true result = (x <= y); //result = true result = (ch1 >= ch2); //result = false since Unicode of ‘a’ is less result = (x != z); //result = false result = (x > y); //result = false 1 2 3 4 5 6 7 8

4. LOGICAL OPERATORS Logical operators are used to construct compound logical expressions. Java uses the following logical operators: ! not && and || or Logical operators take only boolean values as operands. The logical expressions evaluate to either true or false according to the following truth tables: NOT Operand Result true false AND Operand1 Operand2 true false Result OR Operand1 Operand2 true false Result

The complete Unicode table is in lecture W2.2 Identifiers, slide 11 5. LOGICAL OPERATORS EXAMPLE int x = 24, y = 35, z = 20; char ch1 = ‘a’; //Unicode (‘a’) = 97 char ch2 = ‘A’; //Unicode (‘A’) = 65 char ch3 = ‘<‘; //Unicode (‘<‘) = 60 char ch4 = ‘5’; //Unicode (‘5’) = 53 boolean result; result = (x >= y) && (ch1 < ch3);// false && false  result = false result = (ch2 == ch4) || (x > z); // false || true  result = true result = !(ch1 < ch2); // !(false)  true 1 2 3 4 5 6 7 8 9 The complete Unicode table is in lecture W2.2 Identifiers, slide 11

6. ORDER OF PRECEDENCE Parenthesis ( ) inside-out Increment (++), Decrement (--) from left to right * / % + - < > <= >= == != && || = += -= *= /= %=

(dataTypeName) expression 7. TYPE CASTING Type casting is the conversion from a data type to another. SYNTAX (dataTypeName) expression Examples (int) (7.9); // = 7 (double) (25); // = 25.0 (double) (5 + 3); // = (double) (8) = 8.0 (double) (15) / 2; // = 15.0 / 2 = 7.5 (double) (15 / 2); // = (double) (7) = 7.0 (int) (7.8 + (double) (15) / 2); //=(int)(7.8+15.0/2)=(int)(7.8+7.5) = 15 (int) (7.8 + (double) (15 / 2)); //(int)(7.8+7.0) = 14 In Java, arithmetic expressions may have mixed data types. In this case, Java performs implicit type coercion (automatic casting). However, implicit type coercion may generate unexpected results. Avoid using expressions with mixed data types without explicit type coercion (explicit type casting).

7. TYPE CASTING char ch = ‘a’; //Unicode of ‘a’ = 97 int unicode; Examples char ch = ‘a’; //Unicode of ‘a’ = 97 int unicode; unicode = (int)(ch); //unicode = 97 System.out.println (unicode); 97 Examples int x = 98; char ch; ch = (char)(x); System.out.println (ch); b

Self-Check Exercises (1) What is the output of the following program public class Increment { public static void main (String[] args) int sum = 7; System.out.println (sum); System.out.println (sum++); System.out.println (++sum); } 1 2 3 4 5 6 7 8 9 10 11 12 W3.3 Operators2

Self-Check Exercises (2) Assume x, y, and z are int variables; with x = 9, y = 10, and z = 8. What are the values of the three values after the execution of each of the following statements: x++; System.out.println (--y); z *= ++x; Assume x = 7 and y = 9, and z = 22.2. Evaluate the following expressions accordingly: total = x + y + int(z); z /= x; y += (int) z – x; x *= 2 * y + (int) z; W3.1 Operators2

Self-Check Exercises (3) Trace the following statements by filling the table below: public class Trace { public static void main (String[] args) int firstNum; int secondNum; char ch; double z; firstNum = 4; secondNum = 2 + firstNum * 6; z = (firstNum + 1) / 2.0; ch = ‘A’; firstNum = (int) (z) + 8; firstNum = secondNum--; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Line firstNum secondNum ch z W3.1 Operators2