Operators & 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

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.
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
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.
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.
JavaScript, Third Edition
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Chapter 3: Data Types and Operators JavaScript - Introductory.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
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(+=,-=,*=……..)
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.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of 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.
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.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
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.
Operators and Expressions. Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==,
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
Operators and Expressions
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.
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.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment 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
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Computer Programming: C language
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
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.
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
Chapter 3 Operators and Expressions
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
OPERATORS in C Programming
Presentation transcript:

Operators & Expressions 1

Operators C supports rich set of operators. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables.

Types of ‘C’ operators Arithmetic operators Relational operators Logical operators Assignment operators Increment and Decrement operators Conditional operators Bitwise operators Special operators

1. Arithmetic operator + Addition - Subtraction * Multiplication / Division % Modulo division

2. Relational operator C supports six Relational Operators < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to == Is equal to != Is not equal to

3.Logical operators Logical Operators &&, || and ! are the three logical operators. expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero. expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero. !expr1 has a value 1 if expr1 is zero else 0. Example if ( marks >= 40 && attendance >= 75 ) grade = ‘P’ If ( marks < 40 || attendance < 75 ) grade = ‘N’

4. Assignment operators Assignment operators are used to assign the result of an expression to a variable. C has a set of ‘shorthand’ assignment operator (op=): v op =exp; Exam - a += 3 ; a = a + 3 ; Both are same.

Shorthand Assignment operators Simple assignment operator Shorthand operator a = a+1 a + =1 a = a-1 a - =1 a = a* (m+n) a * = m+n a = a / (m+n) a / = m+n a = a %b a %=b

5. Increment and decrement operators. Increment Operator ++ a=10; a++ or a+1=11; Decrement Operator -- b=5; b-- or b-1=4; Similarly increment and decrement operator is used in subscripted variables as: a[ i++]=5; is equivalent to a[ i]=5; i=i+1;

6. Conditional operator C supports a ternary operator i.e. conditional operator. The conditional operator is defined as “ ? : ” The conditional operator works in following format: for example: a=11; b=20; x=(a>b) ? a : b;

7. Bitwise operator C supports bitwise operators for manipulation of data at bit level. Bitwise operators may not be applied to float or double. Bitwise operators are as follows: & bitwise AND | bitwise OR ^ bitwise exclusive OR << shift left >> shift right ~ One’s Complements

8. Special operator C supports some special operators such as: comma operator “,” x=(a=5,b=6) size of operator “sizeof()” Address operator “&” pointer operator “*” member selection operator “. and -> ”

More about operators Unary operator Binary operator Ternary operator

Precedence of operators Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression has to be evaluated. It refers to the order in which c evaluates operators. The evaluation of operators in an arithmetic expression takes place from left to right for operators having equal precedence . Exam. k = 2 * 3/4 + 4/4 + 8–2 + 5/8 ; O/P => 8

Precedence of operators BODMAS RULE- Brackets of Division Multiplication Addition Subtraction Brackets will have the highest precedence and have to be evaluated first, then comes of , then comes division, multiplication, addition and finally subtraction. C language uses some rules in evaluating the expressions and they r called as precedence rules or sometimes also referred to as hierarchy of operations, with some operators with highest precedence and some with least. The 2 distinct priority levels of arithmetic operators in c are- Highest priority : * / % Lowest priority : + -

Associativity of operators Associativity tells how an operator associates with its operands. for eg: 1. The unary minus associates minus with the quantity to its right. 2. The assignment operator = associates from right to left. Hence the expression on the right is evaluated first and its value is assigned to the variable on the left. Associativity also refers to the order in which c evaluates operators in an expression having same precedence. Such type of operator can operate either left to right or vice versa. The operator () function call has highest precedence & the comma operator has lowest precedence All unary , conditional & assignment operators associate RIGHT TO LEFT . All other remaining operators associate LEFT TO RIGHT

Rules for evaluation of expression First parenthesized sub expression from left to right are evaluated. If parentheses are nested, the evaluation begins with the innermost sub expression The precedence rule is applied in determining the order of application of operators in evaluating sub expressions The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression. Arithmetic expressions are evaluated from left to right using the rules of precedence When parentheses are used, the expressions within parentheses assume highest priority

Hierarchy of operators Description Associativity ( ), [ ] Function call, array element reference Left to Right +, -, ++, - -,!,~,*,& Unary plus, minus, increment, decrement, logical negation, 1’s complement, pointer reference, address Right to Left *, / , % Multiplication, division, modulus