Prepared by: Shraddha Modi

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

Operators and Expressions:

UNIT 3 OPERATORS. [Marks- 12]

Operators in C. Staff Incharge: S.Sasirekha

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

Department of Computer Science

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

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

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

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators. Java operators are classified into three categories:

Data Types and Variables in C language

Operators in java Operator operands.

JAVA OPERATORS GENERAL

Chapter 3: Operators, Expressions and Type Conversion

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

Basics of Programming

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

Expression and Operator

Arithmetic Operators. Portability: Printing Numbers

Constants and Variables

UNIT- 3 Introduction to C++

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

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

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

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

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

Reserved Words and Identifiers

Information Science 1

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

Operators & Expressions

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.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 4: Basic C Operators

DEPARTMENT OF MATHS, MJ COLLEGE

Computer System and programming in C

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

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Computers Programming Course 6. Iulian Năstac

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

Expressions and Precedence. Last updated 12/10/18

C Programming Class I

More Programming Constructs -- Introduction

Infix to Postfix Conversion

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

Chapter 4. Operations on Data

Programming for Engineers Iteration

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

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

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

The Arithmetic Operators

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

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

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

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

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

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

SECTION II: LANGUAGE BASICS

Laboratory 3 OPERATORS, EXPRESSIONS, INSTRUCTIONS I. THEORETICAL BACKGROUND

3. Types of Algorithmic and Program Instructions

Data Handing in Python

COMP 208 Computers in Engineering

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.

Operators And Expressions

Introduction to C++ Introduction and History. Characteristics of C++

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

Computer Programming CS F111

Prof. Navrati Saxena TA: Rochak Sachan

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:

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

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

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Prepared by: Shraddha Modi

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

CS313D: ADVANCED PROGRAMMING LANGUAGE

Department of Computer Applications

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

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Information Science 1

JAVA Programming Fundamentals

Fundamentals of Programming CS-110. Lecture 3

INTRODUCTION TO C A PRE-REQUISITE

Chapter 7. Additional Control Structures

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

Work relative to other classes

CT 229. Java Syntax 26/09/2006 CT229

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Flow Chart. The diagrammatic representation shows a solution to a given problem.

UNIT IV INTRODUCTION TO C

Programming in C++ 5. Integral data types

ME 461 C review Session Fall 2009 S. Keres

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

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

Floating Point Arithmetic

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

A Java program contains at least one class definition.

Transcription:

Prepared by: Shraddha Modi

Introduction Operator: An operator is a symbol that tells the Computer to perform certain mathematical or logical manipulations. Expression: An expression is a sequence of operands and operators that reduces to single value. An expression is a formula consisting of 1 or more operators. The operation are represented by operators. Eg: 10+25 is an expression whose value is 35. 1. Simple Expressions 5+6; or a + b; 2. Complex Expressions a * b/c + d; C operators can be classified into a no. of categories.: 1. Arithmetic 2. Relational 3. Logical 4. Assignment 5. Increment and Decrement 6. Conditional 7. Bitwise 8. Special

Arithmetic Operators These are also called binary operators. These operate on two or more operands. C provides 5 basic binary arithmetic operators as it is requires two values to calculate the final answer. Arithmetic operators are also called as Mathematical Operators. Operator Symbol Addition + Subtraction - Multiplication * Division / Modulo division % % cannot be used for floating point data. Note : During modulo division, sign of the result is always the sign of the first operand (the dividend ).

Integer Arithmetic: When the operands in an expression are integers then the expression is an integer expression and the operation is called integer arithmetic. This always yields an integer value. Real Arithmetic / Floating Pont Arithmetic: Floating Point Arithmetic involves only real operands of decimal or exponential notation. Mixed mode Arithmetic: When one of the operands is real and the other is integer the expression is a mixed mode arithmetic expression.

Relational Operator These are used to compare or determine relation among two operands. Operands can be either constant or variable. These are the operators used to Compare arithmetic, logical and character expressions. The value of a relational express is either one or zero. It is 1(one) if specified relation is true and 0(zero) if it is false. Operator Meaning < is less than < = is less than or equal to > is greater than or equal to > = is greater than or equal to = = is equal to! = is not equal to

Logical Operator It is used to combine two or more relational expressions. Logical Operators are used when we want to test more than one condition and make decisions. Operator Meaning && AND a=5, b=7, c= 9 (a<b) && (a<c) (a>b) && (a<c) True(1) False(0) OR (b<a) (a<c) True(1) (b<a) (c<b) False(0)! NOT!(a<b) False(0)!(a>b) True(1)

A B A&&B A B!A 0 0 0 0 1 0 1 0 1 1 1 1 1 1 0 1 0 0 1 0 void main ( ) { clrscr ( ); printf( In 5>3 && 5<10 : %d, 5>3&&5<10); printf( In 8<5 5= =5 : %d, 8<5 5= =5); printf( In!(8 = =8) : %d,!(8= =8)) ; } Output: In 5>3 && 5<10 : 1 In 8<5 5= =5 : 1 In!(8 = =8) : 0

Assignment Operator Used to assign the result of an expression to a variable. = is the assignment operator. In addition C has a set of short hand assignment operators of the form Var Op = Exp ; Operator Symbol Example Result Assignment Operator = a = 10; Addition Assignment Operator += a+=15; a=a+15 Subtraction Assignment Operator -= a-=10; a=a-10 Multiplication Assignment Operator *= a*=n+1 a=a*(n+1) Division Assignment Operator /= a/=n+1 a=a/(n+1) Modulus Assignment Operator %= a%=2; a=a%2

The use of shorthand assignment operators has advantage: 1) what appears on the left-hand side need not be repeated and therefore it becomes easier to write. For example: value(5*j-2) = value(5*j-2) + delta using shorthand assignment operator value(5*j-2) += delta

Increment and Decrement Operators(unary operators ) The increment and decrement operators can be used only with variables, not with constants. The operation performed is to add one to or subtract one from the operand. Operator Increment Operator ++ Decrement Operator -- Meaning ++x or x ++ == > x+=1 == > x=x+1 -- x or x- - == > x-=1 == > x=x-1 A Prefix/pre-increment operator first increment the operand and then the result is assigned to the variable on left. A postfix operator first assigns the value to the variable on the left and the increments the operand. m = 5; m = 5; y = ++m; y = m++ O/P m =6, y=6 O/P m=6, y=5

WAP to show the effect of increment and decrement operators main ( ) { int x = 10, y = 20, z, a ; z= x * y ++; a = x * y ; printf( %d % d\n, z,a); z = x * ++y; a = x * y; printf( %d %d\n, z, a); } OUTPUT: 200 210 220 220

main() { int count = 0, loop; loop = ++count; printf("loop = %d, count = %d\n", loop, count); loop = count++; printf("loop = %d, count = %d\n", loop, count); } Output: loop = 1, count = 1 loop = 1; count = 2

#include<stdio.h> void main() { } int x=1,y=2,z; z=x++ + ++y + y++; printf( z=%d",z); printf( x=%d",x); printf( y=%d",y); Output: z=7 x = 2 y= 4

Conditional Operator The Conditional operator is used to check condition whether true or false. The result will be as per calculated by the statement. Syntax for conditional operators is Expression1? Expression 2 : Expression 3; True Expression 1? Expression 2: Expression 3; False

void main() { int a,b; printf("\n\nenter First No. = "); scanf("%d",&a); printf("\nenter Second No. = "); scanf("%d",&b); (a<b)?printf("\n\n\a\tfirst No. i.e. %d is smaller",a):printf("\n\n\a\tsecond No. i.e. %d is smaller",b); getch(); }

Bitwise operator C Supports a Special operator known as Bitwise operator which provides bit level manipulation. It is used to test the bits or may be shifted right or left. Operator & ^ ~ >> << Description Example: c=a&b, Answer=0 a=1,b=2 so And Operator Or Operator a=0001,b=0010, now 0001 & 0010 = 0000 Excusive OR Operator Compliment Operator Right Shift Operator Left Shift Operator

Bit1 Bit2 Bit1&Bit2 Bit1 Bit2 Bit1 ^ Bit2 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 void main () { int x=15,y=8; printf( Bitwise AND = %d", x&y); printf( Bitwise OR = %d", x y); printf( Bitwise Exclusive OR = %d", x ^ y); printf( Bitwise complement = %d", ~ y); printf( Bitwise left shift = %d", y << 1); printf( Bitwise right shift = %d", y >> 2 ); getch(); } Output: Bitwise AND = 8 Bitwise OR = 15 Bitwise Exclusive OR =7 Bitwise complement = 245 Bitwise left shift = 16 Bitwise right shift = 2

Special operator Comma operator Sizeof operator

Type conversion and Type casting Type conversion : Automatic (lower type to higher type) Rules: 1) If float is assigned to integer, fractional part is truncated. 2) If double is assigned to float, then rounding take place. 3) If long int is assigned to integer, then additional bits are dropped. Type casting : By user Synatx: (typename) expression

Example: void main () { int a = 5,b; float c = 5.5; double d = 4.0; b = a * c + d/10; printf( Ans is = %d", b ); getch(); } Output: Ans is = 27 void main () { int sum = 47,n= 10; float avg; avg = sum / n; printf( Ans is = %f", avg ); avg = (float) sum / n; printf( Ans is = %f", avg ); avg = (float) (sum / n); printf( Ans is = %f", avg ); getch(); } Output: Ans is = 4.0 Ans is = 4.7 Ans is = 4.0