Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

Types and Arithmetic Operators
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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,
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.
Programming C/C++ on Eclipe C Training Trình bày : Ths HungNM.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Chapter 4: Basic C Operators
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
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.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Windows Programming, C.-S. Shieh, KUAS EC, Chapter 3 Operators and Expressions.
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(+=,-=,*=……..)
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
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.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
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.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
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.
Operator Kinds of Operator Precedence of Operator Type Casting.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CSE 220 – C Programming Expressions.
Operators And Expressions
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
Rational Expressions. relational operators. logical operators
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Computing Fundamentals
Relational Operations
Data Types, Identifiers, and Expressions
Variable Declaration, Data types, Expressions
Operators and Expressions
Operators and Expressions
Expressions and Assignment Statements
Data Types, Identifiers, and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Relational Operators Operator Meaning < Less than > Greater than
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
Introduction to Programming – 4 Operators
Expressions.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3 Operators and Expressions
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Expressions and Assignment Statements
OPERATORS in C Programming
Presentation transcript:

Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.

Department of Electronic & Electrical Engineering Expressions An expression is comprises: variables constants function calls (must return a value) operators (unary or binary) operands

Department of Electronic & Electrical Engineering Example: 2.0*x*sin(z*y) constant variable another expression! function 2.0  *x  * ( z*y  sin ) order of evaluation

Department of Electronic & Electrical Engineering Arithmetic Operators Arithmetic operators + * - / can be used with any of the fundamental types ( int char float double ). % can only be used with integer types.

Department of Electronic & Electrical Engineering Pre/Post fix operators Increment / decrement a either before or after the value of the operand a = b++ ; assigns the a with the value of b then increments b. a = ++b ; increments the value of b then assigns the new value to a i++ ; /* increment i */

Department of Electronic & Electrical Engineering Precedence Expressions are usually evaluated from left to right (associativity) If an expression has different operators then precedence rules are applied Brackets can be used to form sub expressions (override precedence) For example arithmetic operators the * / and % operators will be evaluated before + or – e.g. 4+6/2 is equivalent to 4+ (6/2) (4-6)+2 4/6 - 2 (4/6)-2

Department of Electronic & Electrical Engineering Combining different types. Both operands the same type  results is the same. (2/3  0) integer and floating point type t  result will be floating point. e.g. 3.0/2 -> 1.5 When different sizes of the same type are used the result will be the type of the longest operand. These rules are applied to each step as an expression is evaluated e.g. 3/4*4.0 will give the result zero. ( because 3/4  0 ) For the above you could change the order of evaluation e.g. 4.0*3/4 When in doubt use brackets ! e.g. (4.0*3)/4 3/4*4.0 != 3/(4*4.0)

Department of Electronic & Electrical Engineering Examples 4* /8*34 x*y*func(4,5) 5%2 (78+84)*(34-45) int func(int a,int b) { return a*b-7; }

Department of Electronic & Electrical Engineering C and boolean variables (TRUE and FALSE) unlike some other languages C does not have a Boolean type. C uses int any non zero value is interpreted as TRUE. zero is interpreted as FALSE.

Department of Electronic & Electrical Engineering Relational (comparator) operators e.g. 5.0 > 4.0  1 for TRUE 6.0==7.0  0 for FALSE int result 1 or 0

Department of Electronic & Electrical Engineering Logical Operators (combining boolean values) 1 && 0  0 !( 1 || 0)  0 7 && 5  1

Department of Electronic & Electrical Engineering Logical examples. int x=5,y=6,z=7; /* we can declare and initialize at the same time ! */ int result; /* C does not have a boolean type. We use int */ result = x>y; /* result should be 0 which means FALSE */ result = x<y; /* result should be 1 which means TRUE */ result = (x > y) && ( ! z < x) ; /* Hmmm this is a bit tricky */ result = (x > y) && ( ! (z < x)) ; /* I think this is what I meant to write */

Department of Electronic & Electrical Engineering Operator Precedence Higher precedence at the top of the table

Department of Electronic & Electrical Engineering Associativity Consider the expression a ~ b ~ c. operator has left associativity, interpreted as (a ~ b) ~ c right associativity, interpreted as a ~ (b ~ c)

Department of Electronic & Electrical Engineering Associativity Consider If associativity is left to right this is (8-4)+2  6 right to left 8-(4+2)  2

Department of Electronic & Electrical Engineering Bitwise operators. Operates at the level of individual bits of a typebits

Department of Electronic & Electrical Engineering Examples : Bitwise operators int a=5; /* 101 */ int b=3; /* 011 */ int c; c= a & b ; /* bitwise AND c = 001 */ c= a | b; /* bitwise OR c = 111 */ c= a^b; /* XOR c = 110 */ c= ~a; /* complement c = */ c= a << 2 /* left shift by 2 c = */ c= a >> 2 /* right shift 2 c = 001 */

Department of Electronic & Electrical Engineering char x=2; /* */ char y=3; /* */ char z=x && y; /* result is 1 */ char w=x & y; /* result is 2 */ printf(" %d %d \n",z,w); bitwise versus logical operations