Operators & Expressions

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators in C. Staff Incharge: S.Sasirekha

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

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

JAVA OPERATORS GENERAL

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

Lecture 3. More About C

Department of Computer Science

Expressions and Precedence. Last updated 12/10/18

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

Programming in C++ 5. Integral data types

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

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

The Arithmetic Operators

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

Expression and Operator

Fundamentals of Programming CS-110. Lecture 3

Operators. Java operators are classified into three categories:

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

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

Operators And Expressions

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

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

Operators and Expressions:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 3: Operators, Expressions and Type Conversion

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

Information Science 1

Chapter 4: Basic C Operators

LECTURE 3 C++ Basics Part 2

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

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 in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Programming for Engineers: Operators, Expressions, and Statem

Computer Programming CS F111

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Operators in java Operator operands.

UNIT 3 OPERATORS. [Marks- 12]

SECTION II: LANGUAGE BASICS

Basics of Programming

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

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

Data Types and Variables in C language

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

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

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

Constants and Variables

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

COMP Primitive and Class Types. Yi Hong May 14, 2015

Computers Programming Course 6. Iulian Năstac

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Arithmetic Operators. Portability: Printing Numbers

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

DEPARTMENT OF MATHS, MJ COLLEGE

Chapter 3 Structure of a C Program

GO MOCK TEST GO MOCK TEST I

Infix to Postfix Conversion

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

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

Basics of Java Programming

Java enum, casts, and others (Select portions of Chapters 4 & 5)

UNIT- 3 Introduction to C++

Programming for Engineers Iteration

Lecture 3 Tao Wang 1

Prof. Navrati Saxena TA: Rochak Sachan

Chapter 4 Expression & Operators

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

PART II. Computation

Chapter 12 Variables and Operators

Chapter 2: Using Data

Information Science 1

Chapter 2. Lexical Elements & Operators

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

ISA 563 : Fundamentals of Systems Programming

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Lecture 3 Operators MIT AITI

ECE 122 Engineering Problem Solving with Java

QUIZ: What value is stored in a after this

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

C/C++ Programming Lecture 7 Name:

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Structures, Operators

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

Chapter 2, Part III Arithmetic Operators and Decision Making

Chapter 7. Expressions and Assignment Statements ISBN

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

VARIABLES & ASSIGNMENTS

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Add Subtract Multiply Divide

Reserved Words and Identifiers

Engineering Computing I

Fundamentals of Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

Transcription:

Operators & Expressions

Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

Expression An expression is a combination of operands ( constants, variables, numbers) connected by operators and parenthesis. Example : A + B operand operator

Types of expression Arithmetic expression : An expression that involves arithmetic operators. The computed result of this expression is a numeric value. Logical or Boolean expression : An expression that involves relational and/ or logical operators. The result of this expression is a logical value i.e either 1 (TRUE) or 0 (FALSE)

Operators C language is very rich in operators. Four main classes of operators :»Arithmetic»Relational»Logical»Bit wise

Assignment operator It is used to assign variable a value: variable_name = expression; lvalue : In compiler lvalue error messages means that an object on left hand side of assignment operator is missing. rvalue : In compiler rvalue error messages means that expression on right hand side of assignment operator is erroneous.

Two cases of assignment Multiple assignment: int j=k=m=0; Compound assignment: j= j+10; this expression can be written as j + = 10; similarly m= m-100; is equivalent to m - = 100;

Arithmetic operators Following operators are used for arithmetic operations on all built in data types : + (unary plus) - (unary minus) + (addition) - (subtraction) * (multiplication) / (division or quotient) % (modulus or remainder) -- (decrement) ++ (increment)

Binary arithmetic operators A binary operator requires two operands to work with. Addition, subtraction, multiplication, division, and modulus or remainder operator falls in this category. The evaluation of binary operator is LEFT associative that is in an expression operators of same precedence are evaluated from left to right.

Order of evaluation For a complex expression it becomes difficult to make out as to in what order the evaluation of sub expression would take place. In such case we check out the precedence and associativity of operators in the expression.

Precedence Defines the order in which an expression is evaluated operators precedence *, /, % High and are on same level +, - Lower and are on same level

Example int x; x= 7 + 3 * 5; output: x = 22 int x; x= ( 7 + 3) * 5; output: x = 50 int x; x= 7 / 3 * 5; output: x = 10

Modulus operator ( % ) This operator has same priority as that of multiplication and division a % b = output remainder after performing a / b Example: 7 % 10 = 7 7%1 = 0 7 % 2 = 1 7 % 7 = 0

Exercise int y; y = 10 % 4 * 3; Output: 6 int y; y = 3 * 10 % 4; Output:: 2

Important Modulus operator ( % ) : It produces remainder of an integer division. This operator can not be used with floating point numbers. int main( ){ float f_1=3.2, f_2=1.1, f_3 ; f_3 = f_1 % f_2; printf( %f, f_3); return 0; } Output: error at line 3 illegal use of floating point

Invalid arithmetic expressions a * + b : Invalid as two operators can not be used in continuation. a( b * c ) : Invalid as there is no operator between a and b.

Unary arithmetic operators A unary operator requires only one operand or data item. Unary arithmetic operators are:» Unary minus ( - )» Increment ( + +)» decrement ( - - ) Unary operators are RIGHT associative that is they are evaluated from right to left when operators of same precedence are encountered in an expression.

Unary minus ( - ) It is written before a numeric value, variable or expression Its effect is NEGATION of the operand to which it is applied. Example: - 57-2.933 -x -( a * b) 8 * ( - ( a+b))

Increment operator ( ++ ) The increment ( ++ ) operator adds 1 to its operand. n = n +1 ; => ++ n ; Postfix Increment ( n ++) : It increments the value of n after its value is used. Prefix Increment ( ++ n) : It increments the value of n before it is used.

1. x = n++ ; 2. x = ++n ; Where n = 5; Example 1 case 1: It sets the value of x to 5 and then increments n to 6. x = 5 and n= 6 case 2: It increments the value of n and then sets the value of x to 6. x = 6 and n =6

Example sum=x++; sum = ++x; Sum = x; x=x+1; x=x+1; Sum=x;

Example 2 int i =10, net; net = ++i * 5; printf( \n i = %d, i); printf( \n net = %d,net); int i =10, net; net = i++ * 5; printf( \n i = %d, i); printf( \n net = %d,net); Output: i = 11 net = 55 Output: i = 11 net = 50

Decrement operator The decrement ( - - ) operator subtracts 1 from its operand. j = j - 1 ; => -- j; Postfix decrement ( y - -) : In this case value of operand is fetched before subtracting 1 from it. Prefix decrement ( - - y) : In this case value of operand is fetched after subtracting 1 from it.

Example sum=x--; sum = --x; sum = x; x=x-1; x=x-1; sum=x;

Precedence of Arithmetic operators Highest : ++ -- - (unary minus) * / % Lowest + - Operators on same level of precedence are evaluated by the complier from left to right.

Exercise int x= 34.9; printf ( \n\t ++x=%d and x++ = %d,++ x, x++); ++x = 36 and x++ = 34

Relational & Logical operators A relational operator is used to compare two values and the result of such operation is always logical either TRUE ( 1 ) or FALSE ( 0 ). < less than x < y > greater than x > y <= less than or equal to x <= y >= greater than or equal to x >= y == is equal to x = = y!= is not equal to x!= y

Exercise Suppose that i, j, and k are integer variables whose values are 1, 2 and 3, respectively. Expression i < j (i + j) > = k (j + k) > (i + 5) k!=3 j = = 2 Value Interpretation 1 true 1 true 0 false 0 false 1 true

Logical operator A logical operator is used to connect two relational expressions or logical expressions. The result of logical expressions is always an integer value either TRUE ( 1 ) or FALSE( 0 ).(67 > 5) returns 1 (67 = = 5) returns 0 && Logical AND x && y Logical OR x y! Logical NOT!x

Logic AND The output of AND operation is TRUE if BOTH the operands are true. Example: ( 8 < 7 ) && ( 6 > 7) 0 && 0 False ( 0 )

Example 2 (8 > 7) && (6 < 7) 1 && 1 True ( 1 )

Logical OR The result of a logical or operation will be true if either operand is true or if both operands are true. (8 < 7) (6 > 7) is false (8 > 7) (6 > 7) is true (8 > 7) (6 < 7) is true

Logical NOT The Logical NOT (! ) is a unary operator. It negates the value of the logical expression or operand. If value of X = 0! X =?! X = 1! ( 5 < 6 ) ( 7 > 7 ) =???! ( 1) ( 0) =! 1 = 0 -> false! ( 5 > 3) =?? -> 0 -> false!(34 >= 765) =?? -> 1 -> True.

Exercise x = 10 and y = 25 ( x > = 10 ) && ( x < 20 ) ( x >= 10) && ( y < = 15) ( x = = 10 ) && ( y > 20 ) ( x==10) ( y < 20) ( x ==10) &&(! ( y < 20) ) True False True True True

Precedence & Associativity!(logical NOT) ++ -- sizeof( ) Right to left * (multiplication) / ( division) %( modulus) Left to right + - (binary ) Left to right < <= > >= Left to right ==(equal to)!=( Not equal to ) Left to right &&(AND) Left to right (OR) Left to right? : (conditional ) Right to left = += - = * = /= %= Right to left, Left to right highest P R E C E D E N C E lowest

Exercise Suppose that j = 7, an integer variable f = 5.5, a float variable c = w Interpret the value of the following expressions: ( j >= 6) && (c = = w ) ( j >= 6) ( c = = w ) (f < 11) && ( j > 100) (c! = p ) ((j + f) <= 10) f > 5!(f > 5) j < = 3!( j <= 3) j > (f +1)!( j > (f +1)) 1 1 0 1 1 0 0 1 1 0

Suppose that j = 7, an integer variable f = 5.5, a float variable c = w Interpret the value of the following expressions: j + f <= 10 j >= 6 && c = = w f < 11 && j > 100!0 && 0 0!(0 && 0) 0 0 1 0 0 1