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.

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.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
Introduction Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Objectives What are control structures Relational.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
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.
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.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
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:
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
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.
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.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Data Types Declarations Expressions Data storage C++ Basics.
Mathematical Calculations in Java Mrs. C. Furman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Software Technology - I Tools to do operations.....
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.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Operators.
Chapter Two Fundamental Programming Structures in Java Adapted from MIT-AITI slides Variables and Primitive Data Types 1.
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 =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
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
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
Lecture 3 Java Operators.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
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
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Escape Sequences What if we wanted to print the quote character?
OPERATORS (2) CSC 111.
Fundamentals 2.
Expressions and Assignment
Chapter 2: Java Fundamentals
Operators In Java Programming By Rajanikanth B.
Operator King Saud University
Presentation transcript:

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 of type string with initial value equals to the value of constant in B. D-Is the following names are valid, why? Student name 1course course*name Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 1

Java Fundamentals 3 Operators

Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples of operators: // 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 3

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 4

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

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

Example 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.

Modulus 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!

Example: Sum of two integer Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 9 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

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

Increment/Decrement Operators Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 11 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;

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 12

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 13

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

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 15

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=, +=, -=, *=, /=, %= Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 16