ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.

Slides:



Advertisements
Similar presentations
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Advertisements

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.
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.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
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.
Rational Expressions relational operators logical operators order of precedence.
ISBN Chapter 7 Expressions and Assignments Statements.
Operators & Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
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
Variable Declaration, Data types, Expressions
Variable Declarations, Data types, Expressions
Operators and Expressions
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
More about Numerical Computation
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
Chapter 3 Operators and Expressions
Data Types and Expressions
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Herbert G. Mayer, PSU CS Status 7/19/2015
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
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:

ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

Syllabus Operator Precedence and Associativity Arithmetic Operators Relational and Logical Operators Increment and Decrement Operators Assignment Operators Type Conversion (Casting) Memory Access Operators

2 Operator Precedence and Associativity Operators perform very basic arithmetic, comparison, and logic operations. When operators are combined, rules of precedence and associativity determine the evaluation order. Precedence Rules → specifies which operator is evaluated first when operators of different precedence are adjacent Associativity Rules → specifies which operator is evaluated first when operators of the same precedence are adjacent

3 Table 1: C Operator Precedence Table LDescriptionOperatorAssociativityLDescriptionOperatorAssociativity 1 Function call ( ) left-to-right 5 Bitwise left shift << left-to-right Array subscript [ ] Bitwise right shift >> Struct member. 6 Less than < left-to-right Struct dereference -> Greater than > Increment (post) expr++ LT or equal to <= Decrement (post) expr-- GT or equal to >= 2 Indirection * right-to-left 7 Equal to == left-to-right Reference (addr) & Not equal to != Unary plus + 8Bitwise AND & left-to-right Unary minus - 9Bitwise XOR ^ left-to-right Logical negation ! 10Bitwise OR | left-to-right Bitwise NOT ~ 11Logical AND && left-to-right Increment (pre) ++expr 12Logical OR || left-to-right Decrement (pre) --expr 13Conditional ? : right-to-left Cast ( type ) 14Assignment = += -= *= /= %= >>= <<= &= ^= |= right-to-left Size in bytes sizeof 3 Multiplication * left-to-right 15Comma, left-to-right Division / Highest precedence is Level 1. Lowest precedence is Level 15. Use parentheses () to alter the order of evaluation. Modulo % 4 Addition + left-to-right Subtraction -

4 Arithmetic Operators The C language does not support element-by-element operators (e.g., like the.* or.^ operators in MATLAB). OperatorDescriptionExample (Base 10) Unary + Denotes positive value+3 Unary - Denotes negative value -3-3 * Multiplication12 * 4 is 48 / Division12 / 4 is 3 % Modulus (remainder)12 % 4 is 0, 12 % 5 is 2 + Addition is 16 - Subtraction is 8

5 C lacks a built-in “raise to a power” operator. Include the header file. Use the math library function called pow. Syntax: pow(base, exponent) returns base exponent Example: 2.2+(3.5) 3 → *3.5*3.5 -or pow(3.5, 3) Example: y (x+1)/2.8 → pow(y,(x+1)/2.8)

6 Note: Do not use pow() solely to express a numeric constant in base 10 scientific notation. Example: 1.25  10 3 should be written as 1.25e3 in C code. Although 1.25*pow(10,3) works, it is inefficient. Example: 5.3  should be written as 5.3e-5 in C code. Example: 2  3 8 is not base 10, so the e notation cannot be used. Instead, 2*pow(3,8) is correct usage here. Example: 6.5  10 X is base 10 but not a constant. Use 6.5*pow(10,X).

7 Relational and Logical Operators TypeOperatorDescription Relational (comparing values) < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to Logical (Boolean logic) ! Logical negation && Logical AND (short-circuit) || Logical OR (short-circuit)

8 expression  a valid combination of constants, variables, function calls, and operators that, when evaluated, produces a value. The numeric value of a relational or logical expression is: 1 if the expression is true 0 if the expression is false

9 Example: w = 5+sin(3.14) / 2.75; → x = 123 – (10*w); → t1 = w >= 5; → 1 (i.e., true) t3 = x > 0 && w < 5; → 0 (i.e., false) ! (exclamation) is the unary negation operator. A zero operand is converted to 1. A non-zero operand is converted to 0. Example: x = 0; y = 3; !x → 1 !y → 0

10 Increment and Decrement Operators Example: w = 0; z = 15; w++; → 1 ++w; → 2 z--; → 14 OperatorDescriptionExample (x is a variable) ++ Increment (add 1 to operand) ++x or x++ equivalent to x=x+1 -- Decrement (subtract 1 from operand) --x or x-- equivalent to x=x-1

11 Example: Prefix ( ++u, --u ) versus Postfix ( u++, u-- ) n = 5; x = ++n; Result: n → 6 x → 6 (Increment n, get value of n, store in x ) n = 5; x = n++; Result: n → 6 x → 5 (Get value of n, store in x, increment n )

12 Assignment Operators var op= expr is equivalent to var = var op expr where op is +, -, *, / Example: i = i + 2; can be replaced by i += 2; Assignment operators are: += -= *= /= Example: dec -= 2; is equivalent to dec = dec - 2; p *= 5; is equivalent to p = p * 5;

13 Type Conversion (Casting) “lower” type is promoted to “higher” type before the operation proceeds. The result is of the higher type.  If either operand is a long double, convert the other to long double  Otherwise, if either operand is double, convert the other to double  Otherwise, if either operand is float, convert the other to float  Otherwise, convert char and short to int  Then, if either operand is long, convert the other to long

14 The cast operator explicitly converts the type of an expression to a different type. Syntax: (type) n  Returns the value of n cast to the specified type.  The original type of n itself is not altered. Example: char x = 'A'; int y; y = x; // Automatic cast y = (int) x; // Explicit cast

15 Be aware of mixed types when doing arithmetic. Example: float x; int y = 3; x = 1 / 2; x = 1.0 / 2; x = y / 2; x = (float) y / 2; x contains 0.0 x contains 0.5 x contains 1.0 x contains 1.5

16 Memory Access Operators All variables are stored in memory, and each memory location has a unique address. The & operator returns the address of a variable. Example: Suppose x is located at memory address The 32-bit integer number 21 is stored there. OperatorDescriptionExample ( x is a variable) * Indirection *x & Reference (Address of) &x sizeof Size of type (in bytes) sizeof(x) Value of x is 21 Value of &x is 1024 sizeof(x) is 4 AddressContents x