Expression and Operator

Similar documents
Unit-2 (Operators) ANAND KR.SRIVASTAVA

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Operators & Expressions

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Operators. Java operators are classified into three categories:

Expressions and Precedence. Last updated 12/10/18

JAVA OPERATORS GENERAL

Operators in C. Staff Incharge: S.Sasirekha

Operators in java Operator operands.

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Informatics Ingeniería en Electrónica y Automática Industrial

Unit 3. Operators. School of Science and Technology INTRODUCTION

Department of Computer Science

A flow chart is a graphical or symbolic representation of a process.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Lecture 3. More About C

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators and Expressions:

UNIT- 3 Introduction to C++

Operators. Lecture 3 COP 3014 Spring January 16, 2018

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

SECTION II: LANGUAGE BASICS

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Prepared by: Shraddha Modi

Information Science 1

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CSCI 2212: Intermediate Programming / C Chapter 15

Arithmetic Operators. Portability: Printing Numbers

LECTURE 3 C++ Basics Part 2

Beginning C Programming for Engineers

Computer System and programming in C

Chapter 3: Operators, Expressions and Type Conversion

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Programming. Elementary Concepts

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Chapter 12 Variables and Operators

Engineering Computing I

Computers Programming Course 6. Iulian Năstac

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Operators And Expressions

UNIT 3 OPERATORS. [Marks- 12]

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

These are reserved words of the C language. For example int, float, if, else, for, while etc.

ISA 563 : Fundamentals of Systems Programming

Fundamentals of Programming

Arithmetic and Bitwise Operations on Binary Data

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

C/C++ Programming Lecture 7 Name:

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Chapter 12 Variables and Operators

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Applied Computer Programming

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

Fundamental of Programming (C)

Prof. Navrati Saxena TA: Rochak Sachan

PART II. Computation

Lecture 13 Bit Operations

JAVA Programming Fundamentals

Chapter 4. Operations on Data

CT 229. Java Syntax 26/09/2006 CT229

Variables and Operators 2/20/01 Lecture #

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Part I Part 1 Expressions

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

QUIZ: What value is stored in a after this

Programming for Engineers Iteration

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Lecture 02 C FUNDAMENTALS

Differentiate Between Keywords and Identifiers

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Arithmetic and Bitwise Operations on Binary Data

ECEN 468 Advanced Logic Design

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers

Chapter 4: Basic C Operators

Language Reference Manual

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

COMPUTER ARITHMETIC (Part 1)

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

Data Types and Variables in C language

Computer Organization & Systems Exam I Example Questions

Number representations

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Reserved Words and Identifiers

Transcription:

Expression and Operator

Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a value except for the function call that returns void.

Arithmetic Operators Operator Symbol Action Example Addition + Adds operands x + y Subtraction - Subs second from first x - y Negation - Negates operand -x Multiplication * Multiplies operands x * y Division / Divides first by second x / y (integer quotient) Modulus % Remainder of divide op x % y

Assignment Operator x=3 = is an operator The value of this expression is 3 = operator has a side effect -- assign 3 to x 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. For example: x = ( y = 3 ) +1; /* y is assigned 3 */ /* the value of (y=3) is 3 */ /* x is assigned 4 */

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

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

Relational Operators Relational operators allow you to compare variables. They return a 1 value for true and a 0 for false. Operator Symbol Example 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 There is no bool type in C. Instead, C uses: 0 as false Non-zero integer as true

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

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

Bitwise operators The shift operator: x << n Operating on Bits (2) Shifts the bits in x n positions to the left, shifting in zeros on the right. If x = 1111 1111 1111 0000 2 x >> n x << 1 equals 1111 1111 1110 0000 2 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 x >> 1 is 0111 1111 1111 1000 2 (unsigned) x >> 1 is 1111 1111 1111 1000 2 (signed)

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

Shift, Multiplication and Division Multiplication and division is often slower than shift. 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); 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 Operator Precedence ( ) 1 ~, ++, --, unary - 2 *, /, % 3 +, - 4 <<, >> 5 <, <=, >, >= 6 ==,!= 7 & 8 ^ 9 10 && 11 12 =, +=, -=, etc. 14 Precedence level We ll be adding more to this list later on...

An Example What is the difference between the two lines of output? #include <stdio.h> 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 The conditional operator essentially allows you to embed an if statement into an expression Generic Form exp1? exp2 : exp3 Example: z = (x > y)? x : y; This is equivalent to: if (x > y) else z = x; z = y; if exp1 is true (non-zero) value is exp2 if exp1 is false (0), (exp3 is not evaluated) value is exp3 (exp2 is not evaluated)

Comma Operator 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. Example: x = (a++, b++); a is incremented b is assigned to x b is incremented Parenthesis are required because the comma operator has a lower precedence than the assignment operator! The comma operator is often used in for loops.

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