CHAPTER 2 PART #4 OPERATOR 2 nd semester 1432 -1433 King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Expressions, Data Conversion, and Input
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
LESSON 6 – Arithmetic Operators
Chapter 2: Using Data.
OPERATORS.
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.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
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 # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Operators & Expressions
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
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.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter # 2 Part 2 Programs And data
Chapter 7: Expressions and Assignment Statements
ARITHMETIC IN C Operators.
Lecture 3 Java Operators.
Relational Operations
Chapter 7: Expressions and Assignment Statements
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
Assignment and Arithmetic expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
C Operators, Operands, Expressions & Statements
Chapter # 2 Part 2 Programs And data
Expressions and Assignment
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
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Ghadah R. Hadba 1

Operators  Operators are special symbols used for:  arithmetic calculations  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 2

C++ Operators  In C++, there are 5 different groups of operators:  Arithmetic Operators  Assignment Operator  Increment / Decrement Operators  Relational Operators  Logical Operators 3

C++ Operators: Arithmetic Operators 4

 The arithmetic operators in Fig. 2.9 are all binary operators, i.e. operators that take two operands  Each operand can be either a literal, a variable identifier, or an expression.  It is better for the two operands to be of the same data type, otherwise:  The compiler will perform implicit casting (with literal values) and/or,  Explicit casting need to be specified (with variables)  Note the use of various special symbols not used in algebra.( e.g. * and %) 5

Writing Mathematical Formulas in C C Language Elements 6 Always specify multiplication explicitly by using the operator * where needed. Example: b² - 4 a c = Use parentheses when required to control the order of operator evaluation. Example: a + b = c + d Two arithmetic operators can be written in succession if the second is a unary operator Example: a x - ( b + c )= b * b - 4 * a * c a * - ( b + c ) ( a + b ) / ( c + d ) ( a + b ) / ( c + d )

Writing Mathematical Formulas in C++ (Examples) C Language Elements 7 a + b - c a² a x (5b + c) = a + b - c = 1 / ( 1 + a * a) = a * ( 5 * b + c )

C++ Operators: Arithmetic Operators 8

Operators Precedence - Example 9

Parentheses 10  The value of the expression x + y * z * z is always the same as the value of x + ((y * z) * z)

Rules for Expression Evaluation (Examples) C Language Elements 11 z - ( a + b / 2 ) + w * - y ( b * b ) - ( 4 * 4 ) * ( a * c ) A * - ( b + c )

Division Example  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. If the operands are from different data type, especially dividing floating-point number by an integer or vice a versa the result will be a floating-point number 12

Modulus Example Page 13  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! (compileerror)

C++ Operators: Arithmetic Operators  Implicit casting:  is an automatic type conversion by the compiler.  If operands of mixed types are used, the compiler will convert one operand to agree with the other. To do this, it uses a hierarchy of data types: Long double (highest) Double Float Unsigned long int Long int Unsigned int Int (lowest)  boolean, character, wide character, enumeration, and short integer data types are promoted to int 14

C++ Operators: Arithmetic Operators  Implicit casting:  Example(1): The compiler will converts 5 to floating point number because it’s higher than int in the data type hierarchy  Example(2): short a=2000; int b; b=a; the value of a has been promoted from short to int  Example(3): bool f=true; int a; a=f; cout<< a<<endl; 15

C++ Operators: Assignment Operator  We assign a value to a variable using the basic assignment operator (=).  Assignment operator:  Stores a value in a memory.  Basically used in C++ to initialize a variable with a value OR to update it’s content with a new value  It’s syntax is as following It is always a variable identifier. It is either a literal, a variable identifier, or an expression. 16 leftSide = rightSide ;

Mixed Data Types in an Assignment Statement C Language Elements 17 The variable to the left of the equals sign determines the data types of an expression.  If both operands of an expression are integer → then the data type of such an expression will be integer.  If both operands of an expression are double → then the data type of such an expression will be double.  An expression that has operands of both type integer and double is a mixed-type expression. The data type of such a mixed-type expression → will be double.

Mixed Data Types in an Assignment Statement C Language Elements 18 Example: if x is int and y is double then x = 7 * 0.5; y = 7 * 0.5; x = 100/5; y = 100/5;

C++ Operators:  Explicit casting:  Performed by placing (data type) prior the intended variable|value|expression  Example(1): short a=2000; int b; b = (int) a;  Example(2): int value1 = 10, value2 = 4; float result = ( float ) (value1 / value2); OR result= ( float ) value1/( float ) value2; are they the same? 19

20 C++ Operators:  Example: Rounding a number double x; int rounded_x /* code to give x a value omitted*/ rounded_x = (int) (x + 0.5); is rounded to is rounded to Type Cast Explicit casting: converting an expression to a different type by writing the desired type in parentheses in front of the expression or the variable.

Compound Assignment Operators  When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators  C++ allows combining arithmetic and assignment operators into a single operator as following:  Addition/assignment +=  Subtraction/assignment  =  Multiplication/assignment  =  Division/assignment /=  Remainder/assignment %= 21

 The syntax is leftSide Op= rightSide ;  This is equivalent to: leftSide = leftSide Op rightSide ;  Example  X+=1  x=x+1;  x%=5;  x = x % 5;  x*=y+w*z;  x = x*(y+w*z); Compound Assignment Operators Allways it is a variable identifier. It is an arithmetic operator. It is either a literal, a variable identifier, or an expression. 22

Compound Assignment Operators 23

C++ Operators: Increment/Decrement Operators ++ and   are a unary operators that can be applied to variables and increments/decrements the value they hold  x++/++x; is equivalent to x = x+1;  x--/--x; is equivalent to x = x-1; 24

25 C++ Operators: Increment/Decrement Operators Placing the increment/ decrement operator before the variable name differ from placing it after as following:

26 C++ Operators: Increment/Decrement Operators Placing the increment/ decrement operator before the variable name differ from placing it after as following (Example):

27

C++ Operators: Equality and Relational Operators  28

C++ Operators: Equality and Relational Operators  In order to evaluate a comparison between two expressions we can use the relational and equality operators  The relational and equality operators operators in Fig are all binary operators, i.e. operators that take two operands  Each operand can be either a literal, a variable identifier, or an expression.  The result of a equality and relational operations is a Boolean value that can only be true or false, according to its Boolean result. 29

Example Of course, instead of using only numeric constants, we can use any valid expression, including variables. Suppose that a=2,b=3 and c=6 30

Common Programming Errors A syntax error will occur if the operators ==, !=, >= and <= appears with space(s) between its pair of symbols 31 A syntax error will occur when reversing the order of the pair of symbols!=, >= and, =< respectively) Confusing the equality operator == with the assignment operator

C++ Operators: Logical Operators || TF TTT FTF && TF TTF FFF Page 32 SymbolOperator !NOT &&AND ||OR ! TF FT

C++ Operators: Logical Operators Page 33  The Operator ! is an operator to perform the Boolean operation NOT  it has only one operand located at its  Basically, it returns the opposite Boolean value of evaluating its operand  Example:  !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true.  !(6 <= 4) // evaluates to true because (6 <= 4) would be false.

C++ Operators: Logical Operators Page 34  The logical operators && and || are:  binary operators  used when evaluating two expressions to obtain a single relational result.  The operator && corresponds with Boolean logical operation AND.  The operator || corresponds with Boolean logical operation OR.  Example:

Convert Text to C++ code English language: x less than or equal to 0 C++ language:(x<=0) English language: grade not equal to 'A' C++ language: (grade != 'A‘) English language: a single man whose 55 years old or older C++ language ((status=='s‘)&&(gender=='m‘) )&&(age>=55)) English language: an exam score of 90 and above or quiz score of 95 and above C++ language:(exam>=90) || (quiz>=95)

Operator Precedence Page 36  Consider the following expression: x=y*5+y*2/4;  Which operator the compiler will perform first?!  IN C++, order of operations (sometimes called operator precedence) is a rule used to clarify unambiguously which procedures should be performed first in a given  Note that parentheses () can be used to enforce a desired order for performing operations x=y*5+y; x=y*(5+y); 12 21

Page 37 AssociativelyTypeOperatorPrecedence Left-to-rightParentheses()1 Right-to-leftPrefix increment and decrement++, --2 Unary minus- Logical NOT! Right-to-leftType case(type)3 Left-to-rightMultiplication, division, and module *, /, %4 Left-to-rightAddition and subtraction+,-5 Left-to-right >6 Left-to-rightRelational operators,>=7 Left-to-rightEquality operators==, !=8 Left-to-rightLogical AND&&9 Left-to-rightLogical OR||10 Right-to-left+=,-=,*=/=,%=11 Left-to-rightPostfix increment and decrement++, --12 Figure A.1 show the decreasing order of precedence from top to bottom