CSE 220 – C Programming Expressions.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Types and Arithmetic Operators
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
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: Basic C Operators
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.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
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
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.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
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.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
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
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
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
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.
INC 161 , CPE 100 Computer Programming
Assignment and Arithmetic expressions
Variable Declaration, Data types, Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Associativity and Prescedence
Introduction to Programming – 4 Operators
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
Chapter 4: Expression and Operator
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator and Expression
OPERATORS in C Programming
Operator King Saud University
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

CSE 220 – C Programming Expressions

Expressions Expressions: Formulas to compute a value Variables Constants (a + b) * c Operators: tools to build expressions Arithmetic: +, -, *, / Relational for comparisons: >, <, >=, <= Logical, assignment, …

Arithmetic Operators Unary: involves one operand i = +1; j = -1; Binary: requires two operands +: addition -: subtraction *: multiplication /: division %: remainder: 11 % 3 evaluates to 2

What do you think the following code outputs? int a = -4; int b = +a; int c = -a; printf("%d %d %d", a, b, c); -4 4 -4 -4 +4 -4 -4 4 4 -4 -4 4

Arithmetic Operators +,-,*,/: %: both operands must be integers allow int and float operands If both of same type: evaluates as given type If mixed: evaluates as float 1.0 / 2 1 / 2 %: both operands must be integers Cannot use 0 as right hand side of / and %

Operator Precedence Precedence rules: - a + b * c  (- a) + (b * c) 1: unary +, unary – 2: *, /, % 3: binary +, binary – - a + b * c  (- a) + (b * c) i + - j / y * x  i + ( ( (-j) / y ) * x )

Assignment Operators Simple assignment: = Evaluate sqrt(a + b*pow(c, 3) => 16 Add to 5 (the value of x) => 21 Store the result in x Simple assignment: = area = 5.5f; j = 23 + i; x = x + sqrt(a + b*pow(c, 3)); /*pow defined in math.h */ memory area a b c i j x 6 2 5 10 5.5 33 21 What is the value of f after the following statement? float f = 5 /2;

Assignment Operators Compound assignment: uses old value of variable to compute its new value: +=, -=, *=, /=, %= height = height * 2; height *= 2; weight = weight / 2; weight /= 2; Lvalue: an object stored in memory Assignment operators: modify left operand and require an lvalue as left operand 2 = 4; //Error. Can’t store 4 in 2. 2 is not an lvalue equivalent

Assignment Operators Increment/Decrement operators ++ and --: j++; similar to j = j + 1; c--; similar to c = c – 1; Postfix version: int i = 0; printf(“%d”, i++); //Print then increment Prefix version: int i = 0; printf(“%d”, ++i); //Increment then print Postfix operator have higher precedence Is this a valid statement? ++(++x);

Expression Evaluation Expressions can be used as statements i++;//Increments i i+5;//Evaluates i + 5 and discards the result Expressions are evaluated according to precedence order of operators a=b+=c++-d+--e/-f a = b+= (c++) – d + (--e) / (-f) a = b+= (c++) – d + ((--e) / (-f)) a = b+= ( (c++) – d + ((--e) / (-f))) a = (b+= ( (c++) – d + ((--e) / (-f))))

Expression Evaluation C does not specify order of evaluation of subexpressions (a - b)*(c + d) //Evaluate a-b or c+d first? Avoid expressions that use the value of a variable and modify it in the same expression: a = (b+= ( (c++) – d + ((--b) / (-f)))) Use multiple assignment statements instead: x = (--b) / (-f) b += (c++) – d + x a = b;

Expression Evaluation What is the value of c? a = 5; c = (b = a + 2) – (a = 1) If (b = a + 2) is evaluated first: b becomes: 5 + 2 = 7 a becomes: 1 c becomes: 7 – 1 = 6 If (a = 1) is evaluated first: a becomes: 1 b becomes: 1 + 2 = 3 c becomes: 3 – 1 = 2

What do you think the following code outputs? int i = 2; int j = i * i++; print("%d %d", i, j); 3 4 3 6 3 8 undefined

Equality Operators int x = 5, y = 5, z; z = x == y; //z has value 1 Equal to: == Not equal to: != Produce 0 or 1 int x = 5, y = 5, z; z = x == y; //z has value 1 z = x != y; //z has value 0 y = 2; Z = x != y //z has value 1

Relational Operators < <= >= > Produce 0 or 1 < <= >= > Produce 0 or 1 4 >= 4 has value 1 51 < 50 has value 0 Warning!: 5 < 70 < 10  (5 < 70 ) < 10  1 < 10  1

Logical Operators x y x && y x || y 2 1 5 7 9 Produce 0 or 1 Negation: ! (unary): !expr has value 1 if expr has value 0 Logical and: && expr1 && expr2: 1 if both are non zero x > 1 && x < 10 Logical or: || expr1 || expr2: 1 if either is non zero x y x && y x || y 2 1 5 7 9

Bitwise Operators For bit manipulation: Bitwise AND: & Bitwise inclusive OR: | Bitwise exclusive OR: ^ Bitwise complement: ~ Left shift: << Right shift: >> We'll be talking about this much more later in the course. The rest of the lecture is a sneak peak at that material.

Bitwise Operators Binary representation: int i = 22; /* 10110 */ int j = 91; /* 1011011 */ Division by 2 22 = 2*11 = 2 * (2 * 5 + 1) = 2 * (2 * (2*2*1 + 1) + 1) = 24 + 22 + 21 Comparison by powers of 2 (1, 2, 4, 8, 16, 32, 64, …) 22 = 16 + 4 + 2 = 24 + 22 + 21 91 = 64 + 16 + 8 + 2 + 1 =26 + 24 + 23 + 21 + 20 In binary: 1111 = 10000 – 1 = 24 - 1

Bitwise Operators result = i & j; 0000000000010110 0000000001011011 In this example: variables i and j are of type int are represented by 16 bits (2 bytes) Bitwise &: Bitwise ~ (complement): result = i & j; 0000000000010110 0000000001011011 0000000000010010  21 + 24 = 18 result = ~i; 0000000000010110 1111111111101001  65,513

Bitwise Operators Bitwise exclusive or ^: 1 if bits are different, 0 if the same Bitwise inclusive or |: 1 if at least one of the bits is 1 result = i ^ j; 0000000000010110 0000000001011011 0000000001001101  77 result = i | j; 0000000000010110 0000000001011011 0000000001011111  95

Bitwise Operators result = i << 3; 0000000000010110 Left shift: Right shift: result = i << 3; 0000000000010110 0000000010110000  176 result = i >> 2; 0000000000010110 0000000000000101 5

Summary Expressions Operators Operator precedence and expression evaluation Basic Types and type conversion