Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

Operators in C. Staff Incharge: S.Sasirekha

Operators in java Operator operands.

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

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

Operators. Java operators are classified into three categories:

Department of Computer Science

Operators & Expressions

Expression and Operator

JAVA OPERATORS GENERAL

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

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

Expressions and Precedence. Last updated 12/10/18

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

Data Types and Variables in C language

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

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

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

The Arithmetic Operators

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

Fundamentals of Programming CS-110. Lecture 3

Prepared by: Shraddha Modi

UNIT 3 OPERATORS. [Marks- 12]

Chapter 3: Operators, Expressions and Type Conversion

Unit 3. Operators. School of Science and Technology INTRODUCTION

UNIT- 3 Introduction to C++

Constants and Variables

Arithmetic Operators. Portability: Printing Numbers

Computers Programming Course 6. Iulian Năstac

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

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

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

Operators and Expressions:

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

Prof. Navrati Saxena TA: Rochak Sachan

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

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

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

Infix to Postfix Conversion

Operators. Lecture 3 COP 3014 Spring January 16, 2018

DEPARTMENT OF MATHS, MJ COLLEGE

SECTION II: LANGUAGE BASICS

ISA 563 : Fundamentals of Systems Programming

Basics of Programming

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

Information Science 1

CT 229. Java Syntax 26/09/2006 CT229

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

Operators And Expressions

JAVA Programming Fundamentals

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

Lecture 3. More About C

LECTURE 3 C++ Basics Part 2

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

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

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

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

Engineering Computing I

More Programming Constructs -- Introduction

Conditional Statement

Programming in C++ 5. Integral data types

ME 461 C review Session Fall 2009 S. Keres

Variables and Operators 2/20/01 Lecture #

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

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

Part I Part 1 Expressions

Chapter 4: Basic C Operators

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

Beginning C Programming for Engineers

Quick Reference Guide

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.

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Programming. Elementary Concepts

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:

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

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

Java Programming Fundamentals. Visit for more.

Fundamentals of Programming

QUIZ: What value is stored in a after this

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

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

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

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

C/C++ Programming Lecture 7 Name:

Chapter 4. Operations on Data

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

Chapter 7. Expressions and Assignment Statements ISBN

Operators and Expressions

Programming for Engineers Iteration

Chapter 4 Expression & Operators

Java Notes. 10th ICSE. Saravanan Ganesh

C - Basic Introduction

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

Chapter 3 Structure of a C Program

Fall, 2015 Prof. Jungkeun Park

Programming I Lecture 7

OBJECT ORIENTED PROGRAMMING

Reserved Words and Identifiers

Transcription:

Unit-2 (Operators) ANAND KR.SRIVASTAVA 1

Operators in C ( use of operators in C ) Operators are the symbol, to perform some operation ( calculation, manipulation). Set of Operations are used in completion of any task. C is the Programming language to perform some task. & & 2

Operands & Operators Any operation is a combination of two things: 1. Operands 2.Operator Operators works on operands. Operands may be A Data Value A Data Variable A Expression Examples: 10 + 4, a - 3, b + c, (a+b)+(b+10) Here, 10,4,3 are Data values and a,b,c are variables. 3

Types of Operators On the basis of number of operands, operators are divided into three types:- Unary Operators Acts upon one operand Binary Operators Acts upon two operands Ternary Operators Acts upon three operands. 4

Types of Operators:- ( On the basis of features of operators) Increment or Decrement Operator ( Unary) Arithmetic Operators Relational Operators Logical Operators Binary Assignment Operators Bitwise Operators Conditional Operators ( Ternary) Special Operators 5

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

Relational Operators (Relational comparison ) Comparisons can be done with the help of relational operators These consist of:- Operator Meaning < less than <= less than equal to > greater than >= greater than equal to = = Equal to!= Not equal to 7

Logical Operators ( Logical comparison ) Expression which combines two or more relational expression is termed as a logical or compound relational expression The result of these operators is either TRUE or FALSE. Logical expressions are:- && logical AND Logical OR! Logical NOT Ex- if (age>55 && salary <1000) 8

Assignment Operator Used to assign the result of an expression to a variable. ex- int i=20; 1. Simple Assignment e.g. = 2. Compound Assignment e.g. +=, -=, *=, /=, &= 3. Expression Assignment e.g. a=5+(b=8 + (c=2)) - 4 9

Operators continued.. x + = y+1; is same as x = x + (y+1) i.e. += means add y+1 to x Similarly.. a=a-1 a - = 1; a=a*(n+1) a *= n+1; a=a/(n+1) a/= n+1; Q: int z=10; z += 34; z *= 2 ; z -=5; z += z+1; 10

Increment and Decrement C has two very useful operators not generally found in other languages. These are the increment and decrement operators. i.e ++ and -- ++ operator adds 1 to the operand and -- operator subtracts 1 to the operand. Both these operators are unary operators and take the following form. ++m (prefix operator) ; or m++ (postfix operator); --m; or m--; ++m & m++ means m = m+1; -- m & m-- means m = m-1; 11

Ex int m = 5, y ; y = ++m ; printf ( %d%d " y,m ); Result??? int m = 5, y ; y = m++ ; printf ( %d%d " y,m ); Result????? 12

Conditional Operator.. A ternary operator pair?: is available in C to construct conditional expressions of the form. exp1? exp2 : exp3; Operator?: works as follows: exp1 is evaluated first. If it is nonzero (true),then the expression exp2 is evaluated and becomes the value of the expression otherwise vice versa. 13

Example. a=10 ; b=15; x=(a>b)? a : b; It can be written as if ( a > b ) x = a; else x=b; 14

Bitwise Operator. C has distinction of supporting special operators known as Bitwise Operators for manipulation of data at bit level. These are used to test bits. Operators & Meaning bitwise AND bitwise OR ^ << shift left >> shift right bitwise exclusive OR (Ex-OR) ~ One s complement 15

Bitwise Operator. Examples - int a = 4, b = 3 a = 0000 0100 b = 0000 0011 -------------- a & b = 0000 0000 a b = 0000 0111 a ^ b = 0000 0111 In Ex-OR (if both bits are same : 0) (if both bits are diff : 1) a = 10 b = ~ a => ~ (1010) => 0101 16

B Bitwise operators The shift operator: x << n 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 0 if it is an unsigned integer x >> 1 is 0111 1111 1111 1000 2 (unsigned) 17

Operator Precedence( priority) Parenthesis Unary operator ( ~, ++,--,-) Arithmetic ( first - /,%,* then +,-) Shift Left & Right Comparison ( first- >,>=,<,<= then ==,!=) Bitwise AND OR NOT Logical AND OR Conditional Operator Assignment D E C R E A S I N G O R D E R 18

Operator Precedence Operator Precedence Associativity ( ) 1 L to R ~, ++, --, unary - 2 R to L *, /, % 3 L to R +, - 4 L to R <<, >> 5 L to R <, <=, >, >= 6 L to R ==,!= 7 L to R & 8 L to R ^ 9 L to R! 10 L to R && 11 L to R 12 L to R? : 13 R to L =, +=, -=, etc. 14 R to L 19

Special Operators comma(,) {Left to Right} Sizeof(parameter) { show the size of given parameter) Address operator(&) Pointer operator(* ) Member selection operator(. And ->) Q: int z= ( a=3, b = 2, c=4, a+b+c); 20

Problems Q1. a = 2 * 3 % 4 + 4 / 4 + 8 (-2) + 5 / 8 Q2. kk = 3 / 2 * 4 % 3 + 3 / 8 + 3 Q3. int i = 4, j = -1, k = 0, y, z ; y = i + 5 && j + 1 k + 2 ; z = i + 5 j + 1 && k + 2 ; printf ( "\ny = %d z = %d", y, z ) ; 21

Problems Q4. int i = 4, j = -1, k = 0, w, x, y, z ; w = i j k ; x = i && j && k ; y = i j && k ; z = i && j k ; printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ; Q5. int z, x = 5, y = - 10, a = 4, b = 2 z = x ++ - - y * b / a 22

Problems Q6. int a = 10, b b = a++ + ++ a; Print a, b Q7. int a = 4; printf( %d%d, a++ + ++a, a++); Q8. int i = 5; printf( %d, i = ++i = = 6); 23

Problems Q9. int a = 3, b b = ++a + ++ a + ++a; Print a, b Q10. int a = 4, b ; b = a++ + a++ + a++; Print a, b Q11. int i = 5, j ; j = ++i + i++ + ++i; Print i, j 24

Problems Q12. Point out the errors, if any (a) int 314.562 * 150; (b) name = Ajay ; (c) Varchar = 3 ; (d) 3.14 * r * r * h = vol_of_cyl ; (e) area = 3.14 * r ** 2; (f) a = b = 3 = 4 ; 25

Problems Q13. void main() { float a = 5, b = 2; int c; c = a % b; printf( %d, c); Q14. int c=0,d=5,e=10,a; a=c>1?d>1 e>1?10:20:30; printf( a=%d,a); 26

Problems Q15. int x, y, z ; x=y=z=1; z= ++x + ++y + ++z; printf( %d%d%d,x,y,z); Q16. #define x 5+2 void main( ) { int a; a = x * x * x; printf( %d, a); } 27

Problems Q17. void main( ) { int a; a=sizeof( 5.6 ); printf("%d",a); } Q18. int a=0,b=10; if(a=0){ printf("true"); } else{ printf("false"); } 28