Chapter 3 Operators and Expressions

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
OPERATORS.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Operators & Expressions
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
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.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
 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.
Prepared By: K. U. Khimani Assistant Professor IT Department.
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.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Variables, Operators, and Expressions
Expressions and Assignment Statements
Operators & Expressions
CompSci 230 S Programming Techniques
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators And Expressions
Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
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.
Assignment and Arithmetic expressions
Intro to C Tutorial 4: Arithmetic and Logical expressions
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Conversions of the type of the value of an expression
Lecture 3 Expressions Richard Gesick.
Introduction to Programming
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
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.
Data Types and Expressions
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
Primitive Types and Expressions
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

Chapter 3 Operators and Expressions PROGRAMMING IN ANSI C

Operators Arithmetic + - * / % ++ -- Relational 2/24/2019 Operators Arithmetic + - * / % ++ -- Relational < <= > >= == != Logical ! && || Bitwise << >> ~ | ^ & Assignment = (Shorthand Assignment: += -= etc.) Comma , Explicit conversion (type) Conditional ?: Arrow -> Pointer * & Array element [ ] Number of bytes sizeof others ( ) - .

2/24/2019 Expressions An expression is a sequence of operands and operators that produces a single value. This value may be any data type except void.

How to learn operators and expressions? 2/24/2019 How to learn operators and expressions? When we study operators and expressions, we must pay attention to such 5 points: The function of operators. The relation of operators and operands: How many operands does the operator need? Which types does the operator require? The precedence of the operator. The associativity of the operator. The type of the calculated result.

Arithmetic Operators & Expressions 2/24/2019 Arithmetic Operators & Expressions 5 % 2 = -5 % 2 = 5 % -2 = -5 % -2 = 5 % 1 = 5 % 1.0 = 5 / - 2 = 5 / - 2.0 = +:Addition or unary plus e.g. 3+2,+3.5 - :Subtraction or unary minus e.g. 3-2,-3.5 * :Multiplication e.g. 3*2,3.5*2 / :Division e.g. 3/2,3.5/2 If the 2 operands both are integers, the result is an integer and its fractional part is truncated. e.g. 8/5=1 % :Modulo division e.g. 5%2 = 1 Both of the 2 operands must be integers . The sign of the result is the same as the dividend. 1 - 1 1 - 1 O - 2 - 2.5

Arithmetic Operators & Expressions 2/24/2019 Arithmetic Operators & Expressions +,-,*,/,% Precedence: + - (Unary) higher than * / % higher than + - Associativity:+,- (Unary): Right to left others:Left to right

Arithmetic Operators & Expressions 2/24/2019 Arithmetic Operators & Expressions Notice: If the operands are all integers, the result must be an integer. If one of the operands is a real number, the result must be a real number. The modulo division operator cannot used on real numbers but only integers, and the sign of result is the same as the sign of dividend.

Arithmetic Operators & Expressions 2/24/2019 Arithmetic Operators & Expressions An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. e.g. a * b / c + 1.5 – (3.28 + 'f') * (5 % 3)

Arithmetic Operators & Expressions 2/24/2019 Arithmetic Operators & Expressions You must pay attention to : The expression “a multiplied by b” must be written to “a*b” but not “ab”; C doesn’t supply exponential operator, so you can write some multiplication, or you can use mathematic function pow(x,y) in the C function library to express xy; Notice the precedence of those arithmetic operators, and you should reasonably use parentheses.

Arithmetic Operators & Expressions 2/24/2019 Arithmetic Operators & Expressions When expressions include real values, then it is important to take necessary precautions to guard against certain computational errors. e. g. a = 1/3.0; b = a * 3.0; Don’t make any expression divide zero. Avoid data overflow.

Relational Operators & Expressions 2/24/2019 Relational Operators & Expressions < <= > >= == != Value of Relational Expression: 1(True) & 0(False) Precedence: < <= > >= higher than == != Arithmetic operators higher than relational operators. Associativity:Left to right Relational operators are used in the test condition of decision or loop statements to decide the course of action of running program.

Relational Operators & Expressions 2/24/2019 Relational Operators & Expressions int a=3, b=2, c=1, d, f; a > b c == a > b a < b + c d = a > b f = a > b > c 3>2,the value is 1 1==1, the value is 1 b+c=3, the value is 0 d=1 a>b is 1,1>c is 0,so: f = 0

Relational Operators & Expressions 2/24/2019 Relational Operators & Expressions Notice Avoid carrying out “==” and “!=” between real numbers. e.g. 1.0 / 3.0 * 3.0 == 1.0 /*not always 1 */ fabs ( 1.0 / 3.0 * 3.0 - 1.0 ) < 1e-6 Pay attention to distinguish “=” and “==”. e.g. int a = 3, b = 2; a = b == a; /* a = 0 */

Logical Operators & Expressions 2/24/2019 Logical Operators & Expressions ! && || Precedence: ! higher than relational operators higher than && higher than || Associativity: ! : Right to left && , ||: Left to right Value of logical Expression: 1(True) & 0(False) Operands: 0 denotes false, others denote true. e.g. 3.0 && 0 a b !a a&&b a||b 1

Logical Operators & Expressions 2/24/2019 Logical Operators & Expressions int a = 4, b = 5; !a a&&b a||b !a||b 4&&0||2 5>3&&0||8<4-!0 'c'&&'d' !4 => 0 4 && 5 => 1 4 || 5 => 1 0 || 5 => 1 0 || 2 => 1 ((5>3)&&0)||(8<(4-(!0))) = 0||0 => 0 , 99 || 100 => 1

Logical Operators & Expressions 2/24/2019 Logical Operators & Expressions Question:Write the expression of leap year. If the “year” is a leap year, it must satisfy one of the 2 conditions: 1) It can be divided exactly by 4, and it can’t be divided exactly by 100. 2) It can be divided exactly by 400. ( && ) year%4==0 year%100!=0 || year%400==0

Logical Operators & Expressions 2/24/2019 Logical Operators & Expressions Short-circuit When a logical expression is being evaluated, it is not each part linked by && or || all is evaluated. But only when it has to be evaluated, it is done. a&&b /* Only when a is true, b is evaluated. If a is false, b is not evaluated.*/ a||b /* Only when a is false, b is evaluated. If a is true, b is not evaluated.*/ a=1; b=2; c=3; d=4; m=1; n=2; ( m = a >b ) && ( n = c > d ); /* m=0,n=2*/

Assignment Operators & Expressions 2/24/2019 Assignment Operators & Expressions = Format:variable = expression Precedence : = lower than || Associativity: Right to left a = b = 5; int a = b = 5; Wrong! int a = 5, b = 5; Right!

Assignment Operators & Expressions 2/24/2019 Assignment Operators & Expressions  c = 5, b = c, a = b /*a=5, b=5, c=5*/  c = 6, a = 5+c /*a=11, c=6*/  b = 4, c = 6, a = b+c /*a=10, b=4, c=6*/ int a, b, c; a = b = c = 5; a = 5 + (c=6); a = (b=4) + (c=6);

Assignment Operators & Expressions 2/24/2019 Assignment Operators & Expressions “shorthand ” assignment operators: +=, -=, *=, /=, %= variable op = expression; is equivalent to: variable = variable op expression e.g. x * = y + 1; is equivalent to: x = x * (y + 1);

Assignment Operators & Expressions 2/24/2019 Assignment Operators & Expressions int a = 2; a += a -= a*a ;  a += a –= 4  a += (a = a – 4)  a += (a = -2)  a += a  a = a + a  a = (-2) + (-2)  a = -4

Increment and Decrement Operators 2/24/2019 Increment and Decrement Operators (10 / m) ++ Wrong! 10 / m ++ Right! ++ -- m ++; or ++ m; is equivalent to: m = m + 1; m --; or -- m; is equivalent to: m = m - 1; Precedence : same as unary +, unary -, ! Associativity: Right to left

Increment and Decrement Operators 2/24/2019 Increment and Decrement Operators int m=5, n; n = 10 / m++; is equivalent to: n = 10/m; m++; n = 10 / ++m; is equivalent to: ++m; n = 10/m;

Increment and Decrement Operators 2/24/2019 Increment and Decrement Operators j=3; k=++j; j=3; k=j++; j=3; printf("%d",++j); j=3; printf("%d",j++); a=3;b=5;c=(++a)*b; a=3;b=5;c=(a++)*b; j=j+1; k=j; result:k=4, j=4 k=j; j=j+1; result :k=3, j=4  j=j+1; printf(); output:4  printf(); j=j+1; output:3  a=a+1; c=a*b; result :a=4,c=20  c=a*b; a=a+1; result :a=4,c=15

Increment and Decrement Operators 2/24/2019 Increment and Decrement Operators Notice The operand of ++ or -- must be a variable and not any expression or any constant.

Comma Operator & Expressions 2/24/2019 Comma Operator & Expressions expression 1, expression 2, ……, expression n Value of Relational Expression: the value of expression n. Precedence : the lowest Associativity: Left to right

Comma Operator & Expressions 2/24/2019 Comma Operator & Expressions a = 3*4, 5*2 ; b = (a = 3*4, 5*2) ; a=1; b=2; c=3; printf("%d,%d,%d", a, b, c); printf("%d,%d,%d", (a, b, c), b, c); a = 12 a = 12, b = 10 output: 1, 2, 3 output: 3, 2, 3

Implicit Type Conversion 2/24/2019 Implicit Type Conversion short & char int unsigned int long unsigned long float double long double The rules of conversion: P70 In all expressions except assignments, any implicit type conversions are made from a lower size type to a higher size type as shown here:

Implicit Type Conversion 2/24/2019 Implicit Type Conversion During assignment: If expression is real type and the variable is integer type, the value of expression will be truncated its fractional part. If expression is double type and the variable is float type, the value of expression will be round its digits. If expression is long type and the variable is int type, the value of expression will be drop its higher byte.

Implicit Type Conversion 2/24/2019 Implicit Type Conversion int i, x; float f; double d; long L; x = L / i + i * f - d; long float double Notice: in the whole process of type conversion, only the type of the interim value used in evaluation is converted, but all the types of variables are not converted. Except the variable “x” assigned a value, all the values of other variables are not changed. float float double int double

Explicit Type Conversion 2/24/2019 Explicit Type Conversion Form :(type-name) expression Precedence : same as unary +, unary -, !, ++, -- Associativity: Right to left e.g. (int) (x+y)  (int) x + y Notice: like the implicit type conversion, only the type of the interim value used in evaluation is converted, but all the types of variables are not converted. Of course, the values of these variables are not changed. main() { float x, y ; x = 3.6 ; y = (int) x * 2 ; printf("x=%.2f, y=%.2f", x, y); } x=3.60, y=6.00

2/24/2019 Homework Review Questions P78 3.1~3.8 & 3.10 write down in your exercise book Programming Exercises