Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and <. An operator manipulates data objects.

Slides:



Advertisements
Similar presentations
Types of selection structures
Advertisements

Types and Arithmetic Operators
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
JavaScript, Third Edition
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.
Expressions, Data Conversion, and Input
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
April 6, 1998CS102-02Lecture 2-1 Java Operators CS Lecture 2-1 Being a Smooth Operator.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Operators. Today’s Learning Goals … Understand the purpose of JavaScript operators Explore the mathematical operators Find out about the assignment operators.
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
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.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
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.
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
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.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
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.
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
CSE 220 – C Programming Expressions.
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
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;
University of Central Florida COP 3330 Object Oriented Programming
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
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
JavaScript and Ajax (Expression and Operators)
All the Operators 22-Nov-18.
More about Numerical Computation
Chapter 2: Basic Elements of Java
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
OPERATORS in C Programming
Presentation transcript:

Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and <. An operator manipulates data objects called operands. Binary operators manipulates two operands. Operands can be strings, numbers or Booleans.

Assignment Operator The assignment statement evaluates the expression on the right-hand side of the equal sign and assigns the result to the variable on the left-hand side of the equal sign. Ex: var result = 1 + 2;

Precedence and Associativity OperatorDescriptionAssociativity ()parenthesesLeft to Right ++; --auto increment; decrementRight to Left !logical NOTRight to Left *; /; %multiply;divide;modulusLeft to Right +; -add;subtractLeft to Right +concatenationLeft to Right <; <=Less-than; Less-than-equal- to Left to Right >; >=Greater-than; Greater-than- equal-to Left to Right ==; !=equal-to; not equal-toLeft to Right ===; !==identical to; not identical toLeft to Right &bitwise ANDLeft to Right

OperatorDescriptionAssoc. |bitwise ORLeft to Right ^bitwise XORLeft to Right ~bitwise NOTLeft to Right <<bitwise left shiftLeft to Right >>bitwise right shiftLeft to Right >>>bit zero-filled,right shiftLeft to Right &&logical ANDLeft to Right ||logical ORLeft to Right ?:ternary conditionalRight to Left =; +=; -=; *=; /=; %=;assignmentRight to Left,(comma)

Example First JavaScript Sample var result = * 12 / 4; document.write(“result = ” + result + " ");

Example 2 Precedence and Associativity var x = * 12 / 4; document.write( " The result is " + x + " "); var x = ( ) * ( 12 / 4 ); document.write("The result is " + x + " ");

Arithmetic Operators Operators/OperandsFunction x+yaddition x-ysubtraction x*ymultiplication x/ydivision (returns floating- point number) x%ymodulus

Example 3 Arithmetic Operators Arithmetic operators var num1 = 5; var num2 = 7; var result = num1 + num2; document.write(" num1 + num2 = "+ result ); result = result + (10 / 2 + 5); document.write(" 12 + (10 / 2 + 5) = " + result );

Shortcut Assignment Operators OperatorExampleMeaning =var x=1;assign 1 to variable “x” +=x+=2;add 2 to “x” and assign result to “x” -=x-=3;subtract 3 from “x” and assign result to “x” *=x*=4;multiply x by 4 and assign result to “x” /=x/=5;divide x by 5 and assign result to “x” %=x%=6;divide x by 6 and assign remainder to “x”

Example 4 Assignment and Shortcut Operators var num=10; document.write(" " + "num is assigned " + 10); num += 2; document.write(" num += 2; num is " + num ); num -= 1; document.write(" num -= 1; num is " + num); num *= 3; document.write(" num *= 3; num is " + num); num %= 5; document.write(" num %= 5; num is " + num);

Autoincrement and Autodecrement Operators (++) – increment the value of its operand by 1 (--) – decrement the value of its operand by 1 Prefixes: ++x; --x Postfixes: x++; x-- When used with assignment, they are different. y=0; x=5; y=++x; => y = 6 and x = 6 y=x++; => y = 5 and x = 6

Example 5 Auto-increment and Auto-decrement var x=5; var y=0; y = ++x; // add one to x first; then assign to y document.write(" Pre-increment: "); document.write("y is " + y + " "); document.write("x is " + x + " "); document.write(" "); var x=5; var y=0; y=x++; // assign value in x to y; then add one to x document.write(" Post-increment: "); document.write("y is " + y + " "); document.write("x is " + x + " ");

Concatenation Operator (+) – is used as concatenation operator. It is the only operator to manipulate strings. If the operands are a mix of strings and numbers, JavaScript will convert the numbers to string. (+=) – concatenates two strings and assigns the result to x, like: x=“hot”; x +=“dog”; x now is “hotdog”.

Comparison Operators Operator/OperandsFunction x==yx is equal to y x!=yx is not equal to y x>yx is greater than y x>=yx is greater or equal than y x<yx is less than y x<=yx is less or equal than y x===yx is identical to y in value and type x!==yx is not identical to y

Equal and Identical Two strings are equal if they have the same length and same characters in corresponding position. Two numbers are equal if they have the same numeric value. Identical are not only the same value, but also of the same data type: “54”== 54 => true “54”===54 => false true==1 => true true===1 => false

Example 6 Comparing Numbers var x = 5; var y = 4; var result = x > y; document.writeln(" The result is "+ result + ". "); result = x < y; document.writeln( "The result is " + result + ". ");

Comparing strings - Example 7 Comparing Strings var fruit1 = "pear"; var fruit2 = "peaR"; var result = fruit1 > fruit2; document.writeln( " The result is "+ result + ". "); result = fruit1 < fruit2; document.writeln( "The result is " + result + ". "); result = fruit1 === fruit2; // Are they identical; i.e., value and type are the same? document.writeln( "The result is " + result + ". ");

Logical Operators Most often used in “if” statements. Numeric operand is true if it evaluates to any number that is not zero. Operator/OperandsFunction num1 && num2true if num1 is true and num2 is true num1 || num2true if num1 is true or num2 is true ! num1Not num1: true if num1 is false

Logical AND Whatever is suppose to happen is based on two conditions, and both conditions must be met. If the left hand expression of the AND operator is evaluate to zero, the expression is false and the right hand expression is not evaluated.

Example 8 Logical AND Operator var answer = prompt("How old are you? ", ""); if ( answer > 12 && answer < 20 ) { alert("Teenagers rock!"); }

Logical OR Whatever is suppose to happen is based in two conditions, and only one condition must be met. If the expression on the left hand side of the OR operator is evaluated to true, the value of the expression is true and the right hand side expression is not evaluated.

Example 9 Logical OR Operator var answer = prompt("Where should we eat? ", ""); if ( answer == "McDonald's" || answer == "Taco Bell" || answer == "Wendy's"){ alert("No fast food today, thanks."); }

Logical NOT It is used for a negation. The ! is called an unary operator because it has only one operand. !null => true !undefined => true

Example 10 Logical NOT Operator var answer = true; alert("Was ” + answer + “. Now " + !answer);

Example 11 Logical (Boolean) Operators var num1=50; var num2=100; var num3=0; document.write(" num1 && num2 is " + (num1 && num2) + ". "); document.write("num1 || $num2 is " + (num1 || num2) +". "); document.write("! num1 is " + !num1 +". "); document.write("!(num1 && num2) is " + !(num1 && num2) + ". "); document.write("!(num1 && num3) is " + !(num1 && num3) + ". ");

Conditional Operator It is called a ternary operator because it requires three operands. It is a short hand of the if/else conditional statement. x ? y : z If x evaluates to true, the value of the expression becomes y, else the value of the expression becomes z. big = (x > y) ? x : y;

Example 12 Conditional Operator var age = prompt("How old are you? ", ""); var price = (age > 55 ) ? 0 : 7.50; alert("You pay $" + price + 0);

Bitwise Operator Treat their operands as a set of 32 bits. OperatorFunction &Bitwise AND |Bitwise OR ^Bitwise XOR ~Bitwise NOT <<Left shift >>Right shift >>>Zero-fill right shift

Example 13 Bitwise Operators Testing Bitwise Operators var result = 15 & 9; document.write("15 & 9 yields: " + result); result = 15 | 9; document.write(" 15 | 9 yields: " + result); result = 15 ^ 9; document.write(" 15 ^ 9 yields: " + result); result = 9 << 2; document.write(" 9 << 2 yields: " + result); result = 9 >> 2; document.write( " 9 >> 2 yields: " + result); result = -9 >> 2; document.write( " -9 >> 2 yields: " + result); result = 15 >>> 2; document.write( " 15 >>> 2 yields: " + result);

Datatype Conversion JavaScript is a loosely typed language. It automatically converts values when it assigns values to a variable or evaluates an expression. Sometimes you need to force conversions. JavaScript provides String(), Number() and Boolean() Methods.

Example 14 The Conversion Methods Data Conversion var num1 = prompt("Enter a number: ",""); var num2 = prompt("Enter another number: ",""); var result = Number(num1) + Number(num2); // Convert strings to numbers alert("Result is "+ result); var myString=String(num1); result=myString + 200; // String + Number is String alert("Result is "+ result); // Concatenates 200 to the result; alert("Boolean result is "+ Boolean(num2)); // Prints true

The parseInt() Method This Method converts a string to a number. It starts parsing at the beginning of the string and returns all integers until it reaches a non- integer and then stops parsing. In the two-argument format, the first argument is the string to be parsed and the second argument is the number base of the number.

Example 15 Using the parseInt() Function var grade = prompt("What is your grade? ", ""); // grade entered as a string grade=parseInt(grade); // grade converted to an integer document.write("grade type is " + typeof(grade)); grade+=10; document.write(" After a 10 point bonus, your grade is “ + grade + "! ");

The parseFloat() Method It is exactly like the pasrseInt() Method, except that it returns a floating point number.

Example 16 Using the parseFloat() Function var temp = prompt("What is your temperature? ", ""); temp=parseFloat(temp); if(temp == 98.6){ alert("Your temp is normal"); } else{ alert("You are sick!"); }

The eval() Method The eval() method evaluates a string of JavaScript statements and evaluates the whole thing as a little program, returning the result of the execution.

Example 17 The eval() Function var str="5 + 4"; var num1 = eval(str); var num2 = eval(prompt("Give me a number ", "")); alert(num1 + num2);

Special Operators Will be discussed later in the course. Some examples:, (comma) delete function instanceof new this void