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

Similar documents
Unit 3. Operators. School of Science and Technology INTRODUCTION

Information Science 1

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

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

Operators. Java operators are classified into three categories:

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

Operators in C. Staff Incharge: S.Sasirekha

Chapter 3: Operators, Expressions and Type Conversion

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

Expressions and Precedence. Last updated 12/10/18

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

LECTURE 3 C++ Basics Part 2

Reserved Words and Identifiers

JAVA OPERATORS GENERAL

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 4: Basic C Operators

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

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

Operators and Expressions:

UNIT- 3 Introduction to C++

Programming in C++ 5. Integral data types

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

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

SECTION II: LANGUAGE BASICS

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

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

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Department of Computer Science

Computers Programming Course 6. Iulian Năstac

Operators & Expressions

Fundamentals of Programming

Basics of Java Programming

Data Types and Variables in C language

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

The Arithmetic Operators

Information Science 1

Prefix/Infix/Postfix Notation

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

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

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

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

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Review of the C Programming Language for Principles of Operating Systems

More Programming Constructs -- Introduction

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Variables and literals

Programming Lecture 3

ISA 563 : Fundamentals of Systems Programming

Arithmetic Expressions in C

UNIT 3 OPERATORS. [Marks- 12]

Fundamental of Programming (C)

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Bits, Words, and Integers

Chapter 2: Using Data

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Lecture 3 Tao Wang 1

Operators And Expressions

Operators in java Operator operands.

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

Fundamentals of Programming Session 7

PART II. Computation

Prof. Navrati Saxena TA: Rochak Sachan

Engineering Computing I

The C++ Language. Arizona State University 1

Work relative to other classes

DEPARTMENT OF MATHS, MJ COLLEGE

Part I Part 1 Expressions

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

ME 461 C review Session Fall 2009 S. Keres

Chapter 7. Additional Control Structures

Basic Operations jgrasp debugger Writing Programs & Checkstyle

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Prepared by: Shraddha Modi

QUIZ: What value is stored in a after this

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Review of the C Programming Language

LECTURE 02 INTRODUCTION TO C++

Applied Computer Programming

Chapter 12 Variables and Operators

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

CT 229. Java Syntax 26/09/2006 CT229

CS102: Variables and Expressions

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Declaration and Memory

Transcription:

Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships comprise two major entities Operation be preformed Entities upon which operations being preformed Operations typically represented by shorthand symbol called operator We are familiar with basic operators for Add + Subtract - Multiply * or x Divide / or Entities upon which operations performed Called operands Most familiar operands are numbers Sometimes use letters or characters Characterizing Operators We use number of different terms to characterize operators Arity Precedence Associativity Arity Identifies the number of operands involved in single operation Typical arity is binary Operator operates on two operands x+y 1+2 In expressions like 1 + 2 + 3 + 4 Seem to involve many operands However expression really involves operations on pairs of numbers Other common arities - 1 of 13 -

Unary Operator operates only on single operand Ternary Operator operates on three operands Precedence When several operators present in expression Need to know order in which each is evaluated Operators of higher precedence evaluated before those of lower Associativity Goes hand in hand with precedence With several operators present in expression If all have same precedence Need to know to which operator an operand belongs Two possibilities Operator on right or one on left Grouping with right or left operator Depends upon associativity of operator C Language and Operators A First Look The C language supports Three types of operations Arithmetic Relational Logical Three types of arity Specifies the number of operands Unary Binary Ternary Two types of associativity Left Right - 2 of 13 -

Precedence Specifies the order in which operators are evaluated a + b c =? Evaluated as evaluated first + evaluated second a + (b c) Can control order of evaluation with parentheses Evaluation from inside out Good Style Wise to use parentheses for Clarity Ensuring expression evaluated as desired Associativity Most C operators associate (evaluated) Left to right Consider expression 6 + 7 + 8 (6 + 7) + 8 by convention 6-7 - 8 (6-7) - 8 by convention Both cases 7 has operators on Left Right Must decide Which operator gets the operand Define operator + Associate to left Because operand with + on both sides taken by operator on left - 3 of 13 -

Define operator = Associate to right a = b = c a = (b = c) Operand b with = on both sides taken by operator on right All operators with same precedence have same associativity Evaluation If two operators have same precedence Operand grouped according to associativity a + b - c * d / e =? Evaluated as (a + b) - ((c * d) / c) C Language and Operators A Further Look Shortcut Operators C language supports shorthand notation For expressing certain operations Familiar with standard notation For binary operators we have variable1 = variable2 operation variable3 In some cases result or variable1 is same variable as One of the operands When such is case Can using shortcut notation to write expression in form syntax variable1 operation = variable2 x += y; // x = x + y x += 1 // x = x + 1-4 of 13 -

Autoincrement Autodecrement Operators Autoincrement and autodecrement operators Increment or decrement operand by 1 Come in two flavours Prefix and postfix syntax prefix operation variable1 postfix variable1 operation Operation performed in two steps Difference between two different forms Prefix operation variable1 1. Operation performed 2. Result returned Postfix operation variable1 1. Result returned 2. Operation performed int x = 4; Prefix Autoincrement ++x; // x = x + 1 x = 5 // return 5 Autodecrement --x; // x = x - 1 x = 3 // return 3 Postfix Autoincrement x++; // return 4 // x = x + 1 x = 5-5 of 13 -

Autodecrement x-- ; // return 4 // x = x - 1 x = 3 In many cases use of prefix or postfix form matters little In other cases can make a difference Caution: When using prefix or postfix forms of autoincrement or autodecrement operators Always Stop to think about results Make certain returned value is what you are looking for C Arithmetic Operators Let s now put some of these operators to work Examine group used to perform arithmetic operations On variables Operators are the same for both types Integer Floating-point Basic C operators are: + add - subtract / divide * multiply and they work as you would expect Let's start by declaring some variables int a = 20; int b = 25; int c; - 6 of 13 -

We next write c = a * 2/b + 15; Variable c gets a times 2 Divided by b Plus 15 Which is really 40 divided by 40 We expect the variable c to be 1 However, if we include these four lines of code In a program then compile and run it We will print the number 16 will appear Why did that happen? The answer to this question lays in concept Called operator precedence Or simply precedence Your textbook contains C Operator Precedence and Associatively table Will talk more about operators later Operators in C evaluated in a specific order From the Associatively table We see division operator appears in the table Closer to the top than the addition operator Means compiler will Perform division Before addition Perform multiplication Before division Let's re-look at the example In the light of precedence 1. a is multiplied by 2 Because multiplication comes before division or addition This is 20 times 2 Which is 40. - 7 of 13 -

2. Next 40 is divided by b Because division comes before addition This is 40 divided by 25 Which is 1.6 Warning Here: Since integers do not have fractional parts 0.6 is simply dropped leaving 1 Will discuss shortly 3. Finally, 15 is added to 1 To give 16 If we wish evaluation to proceed in different order Can over ride precedence Using parentheses To force b plus 15 to be done Before the division Enclose those terms in parentheses We now have c = a * 2/ (b + 15); Doing this changes evaluation order Because the grouping parentheses operator Comes before any arithmetic operator Means the inside of the parentheses Must be evaluated first We can nest parentheses to any depth To achieve whatever desired evaluation order The line of code now evaluated as follows 1. 15 is added to b Because the parentheses must be evaluated first This is 25 plus 15 Which is 40. 2. Next a is multiplied by 2 Because multiplication precedes division This is 20 times 2, or 40 3. Finally 40 is divided by 40 to give 1-8 of 13 -

Now we ask If precedence determines which operator is applied first What happens when all of the operators in a line of code Have the same precedence a + b c Does the compiler evaluate a + b or b c first? The answer is associativity If we look up addition and subtraction In the C Operator Precedence and Associativity table We will see the associatively is L-R Means operator on the left Will be the first to be evaluated In the above line a + b will be evaluated first Then c will be subtracted This is important because it can Affect your mathematical calculations. Integer Division In the arithmetic example above Was one step in which 40 is divided by 25 Commonly expected answer is 1.6 Since integers do not have fractional parts 0.6 is simply dropped In C we would read that 40 divided by 25 is 1 because there is only one 25 in 40 With a remainder of 15 Because the two operands are integers Compiler expresses result as integer 0.6 can't be expressed as integer - 9 of 13 -

If our application needs non integer numbers We can use floating-point variables When using floats Must remember may not be able to display complete result Let's divide 10 by 3 Result will be 3.33333333333333 Until we get tired of writing 3s Computer cannot store infinite number of 3s So some get dropped Stored number is then rounded Up or down This means your floating-point numbers are really approximations We introduce rounding error into our calculations For this reason Most mathematical operations in C use Integers rather than floating-point numbers The Modulus Operator % Recall our earlier discussion of integer types We ve seen integer division Could give Whole number part Fractional part Fractional part Arises because of remainder Produced during division operation Such operation important enough In mathematics We ve defined Special name Special operator symbol - 10 of 13 -

Operation Called modulus or mod Operator Called modulus operator Written as percent sign % Also called remainder operator We ll learn about uses In later lessons Learning concept now Let s apply the mod operator to the following ints int number = 15; printf( %d\n, number %3 ); // prints 0 3 divides 15 5 times with 0 remainder printf( %d\n, number %5 ); // prints 0 5 divides 15 3 times with 0 remainder printf( %d\n, number %10 ); // prints 5 10 divides 15 one time with 5 remainder printf( %d\n, number % 2 ); // prints 1 2 divides 15 7 times with 1 remainder As another example We can use the modulus operator to convert ounces to pounds and ounces int weight = 1610; int pounds = 1610/16; int ounces = 1610 % 16; // weight in ounces // gives 100 pounds // gives 10 ounces Table of Operators Table of C operators follows Those grouped together have same precedence Precedence decreases from top to bottom - 11 of 13 -

Operator Arity Associativity Description x[i] unary left Array Subscripting a[i] f(x) unary left Function Call display(values). unary left Direct Selection rectangle.width -> unary left Indirect Selection rectangle->width ++, -- unary left Postfix Increment, Decrement a++, b-- ++, -- unary right Increment, Decrement ++a, --b sizeof (type) unary right size of variable or type in bytes sizeof a, sizeof (char) ~ unary right Bitwise NOT ~a! unary right Logical NOT! a -, + unary right Arithmetic Negative, Plus -a, +a & unary right Address of &a * unary right Indirection *a *, /, % binary left Arithmetic multiply divide modulus a * b a / b a % b +, - binary left Arithmetic addition subtraction a + b a - b <<, >> binary left Left and Right Shift a << 3, 1 >> b <, >, <=, >= binary left Test for Inequality less than greater than less than or equal greater than or equal a < b a > b a <= b a >= b ==,!= binary left Test for Inequality equal not equal a == b a!= b & binary left Bitwise AND a & b ^ binary left Bitwise XOR a ^ b binary left Bitwise OR a b && binary left Logical AND a && b binary left Logical OR a b?: ternary right Conditional a? b : c = binary right Assignment a = b +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, = binary right Assignment Add to Subtract from Divide by Multiply by a += 2 a -= 3 a /= 4 a *= 2-12 of 13 -

Assign Remainder Shift Left Shift Right Bitwise AND Bitwise OR Bitwise XOR a %= 4 b <<= 3 b >>= 4 b &= c b ^= d b = e b, c,d,e are a bit patterns, left binary Sequential Evaluation for (i=0, j=10; i<5; i++, j--) Summary Following this lesson we Should understand basic terms for characterizing operators Understand arity Understand associativity and precedence Know type of operators supported by C language How to use parentheses to force order of expression evaluation Examined basic arithmetic operators - 13 of 13 -