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.

Slides:



Advertisements
Similar presentations
1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.
Advertisements

LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
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.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
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,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
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.
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.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
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.
Review of Lectures 6, 7, 8 and 10. Compilation (1) Compilation translates your source code (in the file hello_world.c) into object code (machine dependent.
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.
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.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
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.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
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.
Operators & Expressions
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Operators.
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.
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.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
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.
Operators & Expressions
CSE 220 – C Programming Expressions.
Operators And Expressions
Side Effect Operators Changing the value of variables
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Lecture 7: Repeating a Known Number of Times
University of Central Florida COP 3330 Object Oriented Programming
INC 161 , CPE 100 Computer Programming
Variable Declaration, Data types, Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 5 from (Chapter 4, pages 73 to 96)
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
Herbert G. Mayer, PSU CS Status 7/19/2015
Assignment Operators Topics Increment and Decrement Operators
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator and Expression
OPERATORS in C Programming
OPERATORS in C Programming
Presentation transcript:

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 by data and operators u An expression in C usually has a value –except for the function call that returns void.

Arithmetic Operators OperatorSymbol Action Example Addition + Adds operandsx + y Subtraction - Subs second from firstx - y Negation - Negates operand-x Multiplication * Multiplies operandsx * y Division / Divides first by secondx / y (integer quotient) Modulus % Remainder of divide opx % y

Assignment Operator u x=3 –= is an operator –The value of this expression is 3 –= operator has a side effect -- assign 3 to x u The assignment operator = –The side-effect is to assign the value of the right hand side (rhs) to the left hand side (lhs). –The value is the value of the rhs. u For example: x = ( y = 3 ) +1; /* y is assigned 3 */ /* the value of (y=3) is 3 */ /* x is assigned 4 */

Compound Assignment Operator u Often we use “update” forms of operators –x=x+1, x=x*2,... u C offers a short form for this: –Generic Form variable op= expr equivalent to variable = variable op expr –Update forms have value equal to the final value of expr v i.e., x=3; y= (x+=3); /* x and y both get value 6 */ OperatorEquivalent to: x *= yx = x * y y -= z + 1y = y - (z + 1) a /= ba = a / b x += y / 8x = x + (y / 8) y %= 3y = y % 3

Increment and Decrement u Other operators with side effects are the pre- and post- increment and decrement operators. –Increment:++++x, x++ v ++x is the same as : (x = x + 1) –Has value x old +1 –Has side-effect of incrementing x v x++ –Has value x old –Has side-effect of incrementing x –Decrement -- --x, x-- v similar to ++

Relational Operators u Relational operators allow you to compare variables. –They return a 1 value for true and a 0 for false. OperatorSymbolExample Equals==x == y NOT x = y Greater than>x > y Less than<x < y Greater/equals>=x >= y Less than/equals<=x <= y Not equal !=x != y u There is no bool type in C. Instead, C uses: –0 as false –Non-zero integer as true

Logical Operators u && AND u || OR u ! NOT !((a>1)&&(a -10))

Operating on Bits (1) u C allows you to operate on the bit representations of integer variables. –Generally called bit-wise operators. u All integers can be thought of in binary form. –For example, suppose ints have 16-bits v = = FFF0 16 = u In C, hexadecimal literals begin with 0x, and octal literals begin with 0. v x=65520;base 10 v x=0xfff0;base 16 (hex) v x= ;base 8 (octal)

Operating on Bits (2) Bitwise operators u The shift operator: –x << n v Shifts the bits in x n positions to the left, shifting in zeros on the right. v If x = x << 1 equals –x >> n v Shifts the bits in x n positions right. –shifts in the sign if it is a signed integer (arithmetic shift) –shifts in 0 if it is an unsigned integer v x >> 1 is (unsigned) v x >> 1 is (signed)

Operating on Bits (3) u Bitwise logical operations –Work on all integer types v & Bitwise AND x=0xFFF0 y=0x002F x&y=0x0020 v | Bitwise Inclusive OR x|y=0xFFFF v ^ Bitwise Exclusive OR x^y=0xFFDF v ~ The complement operator ~ y= 0xFFD0 –Complements all of the bits of X

Shift, Multiplication and Division u Multiplication and division is often slower than shift. u Multiplying 2 can be replaced by shifting 1 bit to the left. n = 10 printf(“%d = %d”, n*2, n<<1); printf(“%d = %d”, n*4, n<<2); …… u Division by 2 can be replace by shifting 1 bit to the right. n = 10 printf(“%d = %d”, n/2, n>>1); printf(“%d = %d”, n/4, n>>2);

Operator Precedence Operator Precedence level ( ) 1 ~, ++, --, unary - 2 *, /, % 3 +, - 4 > 5, >= 6 ==, != 7 & 8 ^ 9 |10 &&11 ||12 =, +=, -=, etc.14 uWe’ll be adding more to this list later on...

An Example u What is the difference between the two lines of output? #include int main () { int w=10,x=20,y=30,z=40; int temp1, temp2; temp1 = x * x /++y + z / y; printf ("temp1= %d;\nw= %d;\nx= %d;\ny= %d;\nz= %d\n", temp1, w,x,y,z); y=30; temp2 = x * x /y++ + z / y; printf ("temp2= %d;\nw= %d;\nx= %d;\ny= %d;\nz= %d\n", temp2, w,x,y,z); return 0; }

Conditional Operator u The conditional operator essentially allows you to embed an “if” statement into an expression u Generic Form exp1 ? exp2 : exp3 if exp1 is true (non-zero) value is exp2 (exp3 is not evaluated) if exp1 is false (0), value is exp3 (exp2 is not evaluated) u Example: z = (x > y) ? x : y; v This is equivalent to: if (x > y) z = x; else z = y;

Comma Operator u An expression can be composed of multiple subexpressions separated by commas. –Subexpressions are evaluated left to right. –The entire expression evaluates to the value of the rightmost subexpression. u Example: x = (a++, b++); v a is incremented v b is assigned to x v b is incremented –Parenthesis are required because the comma operator has a lower precedence than the assignment operator! u The comma operator is often used in for loops.

Comma Operator and For Loop u Example: u int i, sum; u for (i=0,sum=0;i<100;i++){ u sum += i; u } u printf(“ = %d”, sum);