Operators and Expressions:

Similar documents
Department of Computer Science

Data Types and Variables in C language

Operators in C. Staff Incharge: S.Sasirekha

UNIT 3 OPERATORS. [Marks- 12]

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

Basics of Programming

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

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

Prepared by: Shraddha Modi

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

Programming in C++ 5. Integral data types

Operators And Expressions

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Computer System and programming in C

Operators in java Operator operands.

Operators & Expressions

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

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

Chapter 4: Basic C Operators

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

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

Engineering Computing I

Arithmetic Operators. Portability: Printing Numbers

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

Expressions and Precedence. Last updated 12/10/18

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Fundamentals of Programming

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

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

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.

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

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

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

More Programming Constructs -- Introduction

Reserved Words and Identifiers

UNIT- 3 Introduction to C++

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

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

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

JAVA OPERATORS GENERAL

Information Science 1

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

Operators. Java operators are classified into three categories:

Fundamental of Programming (C)

Chapter 3: Operators, Expressions and Type Conversion

Lecture 3. More About C

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Programming for Engineers Iteration

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

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

Structured Programming. Dr. Mohamed Khedr Lecture 4

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Computers Programming Course 6. Iulian Năstac

Expression and Operator

Computer Programming CS F111

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

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

Chapter 3 Structure of a C Program

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

Chapter 2: Using Data

Unit-2 (Operators) ANAND KR.SRIVASTAVA

DEPARTMENT OF MATHS, MJ COLLEGE

C Programming

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C Programming Class I

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

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

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

The Arithmetic Operators

LECTURE 3 C++ Basics Part 2

Structures, Operators

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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:

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

Infix to Postfix Conversion

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

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

Programming for Engineers: Operators, Expressions, and Statem

ANSI C Programming Simple Programs

Constants and Variables

Structured programming. Exercises 3

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

Visual C# Instructor s Manual Table of Contents

COMP 208 Computers in Engineering

C - Basic Introduction

Learning to Program with Haiku

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

Chapter 2. Outline. Simple C++ Programs

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

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

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

Advantages of writing algorithm

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

Transcription:

Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence and associativity. 1

Objectives To be able to construct and evaluate expressions. To master operator precedence and associativity To understand implicit type conversion and explicit type conversion. 2

Introduction An operator is a symbol that tells the computer to perform certain manipulations. An expression is a sequence of operands and operators that reduces to a single value. C operators can be classified into a number of categories. Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators 3

4 Arithmetic operators The arithmetic operators in C Operator meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % modulo division

5 Arithmetic operators Note:, Integer division truncates remainder The % operator cannot be applied to a float or double. The precedence of arithmetic operators Unary + or - * / % + -

#include<stdio.h> main() { int months, days ; printf ( "Enter days \n ) ; scanf ( "%d, &days ) ; months = days / 30 ; days = days % 30 ; printf ( "Months =%d Days= %d \n", months, days); } 6

7 Arithmetic expressions An arithmetic expression is a combination of variables, constants, and operators. For example, a*b-c a*b-c (m+n)(x+y) (m+n)*(x+y) ax 2 +bx+c a*x*x+b*x+c

8 Mathematical functions Mathematical functions such as cos, sqrt, log, etc. are frequently used in analysis of real-life problems. Table 3.9 on page 72 lists some standard math functions. We should include the line #include<math.h> in the beginning of the program.

9 Mathematical functions for example the roots of ax 2 +bx+c =0 are x b b 2 4ac 2a x1= ( -b + sqrt ( b*b - 4*a*c ) ) / ( 2 * a ) x2= ( -b sqrt ( b*b - 4*a*c ) ) / ( 2 * a )

10 Relational Operators The relational operators in C are : Operator Meaning < less that <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to

Relational Operators A relational expression yields a value of 1 or 0. 5 < 6 1-34 + 8 > 23-5 0 if a=3, b=2, c =1; then a > b > c is? the associativity of relational operators is left right table3.8 on page71 11

Relational Operators The relational operators >, >=, < and <= have the same precedence. Just below them in precedence are the equality operators: = = and!=. Relational operators have lower precedence than arithmetic operators, so an expression like i < lim-1 is taken as i < (lim-1). 3 >= 2 = = -4 < 0 12

Logical operators C has the following three logical operators && meaning logical and meaning logical or! meaning logical not ( unary operator ) Expressions connected by && or are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. 13

14 Logical operators op-1 op2 op-1&&op-2 op-1 op-2!op-1 Non-zero Non-zero 1 1 0 Non-zero 0 0 1 0 0 Non-zero 0 1 1 0 0 0 0 1

Logical operators The precedence of && is higher than that of, and both are lower than relational operators, so 3 < 5 && -3 < -5 3 > 2 char ch; to decide whether ch is an uppercase, ch >= A && ch <= Z to decide whether a year is leap year, year% 4 ==0 && year % 100!= 0 year % 400 = = 0 15

Assignment operators Assignment operators are used to assign the result of an expression to a variable. C has a set of shorthand assignment operators of the form v op= exp; where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator op= is known as the shorthand assignment operator. The assignment statement v op= exp; is equivalent to v = v op (exp) ; For example, x += y + 1; This is same as the statement x = x + ( y + 1 ) ; 16

17 Assignment operators Statement with simple assignment operator Statement with shorthand operator a = a+1 a += 1 a = a-1 a -= 1 a = a * (n+1) a *= n+1 a = a / (n+1) a /= n+1 a = a % b a %= b

Assignment operators The use of shorthand assignment operators has three advantages: 1. What appears on the left-hand side need not be repeated and therefore it becomes easier to write. 2. The statement is more concise and easier to read. 3. The statement is more efficient. 18

Assignment operators int a=12, n=5; (1) a += a (2) a -= 2 (3) a *= 2 + 3 (4) a /= a + a (5) a % = ( n %= 2 ) (6) a += a -= a *= a 19

Increment and decrement operators C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1. The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. 20

for example, if n is 5, then x = n++; is equivalent x = n; n++; but x = ++n; is equivalent n++; x = n; The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal. 21

The increment and decrement operators can be used in complex statements. Example: m=n++ -j +10; Consider the expression m = - n++ ; The precedence of ++ and operators are the same as those of unary + and -. The associatively of them is right to left. m = - n++; is equivalent to m = - (n++) 22

suppose, int a, b, c ; a = b = c = 1; After execution the following statements, what are the values of the expression and variables. (1) a>b && b>c++; (2) a-- c++; (3)!a && b++; (4) ++a && ++b && ++c; (5) ++a && --b && ++c; 23

Conditional operator a ternary operator pair? : is available in C to construct conditional expressions of the form expr1? expr2 : expr3 the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is evaluated. 24

Special operators 1. The Comma Operator The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left to right and the value of right-most expression is the value of the combined expression. For example, the statement value = (x=10, y=5, x+y); first assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 to value. Since comma operator has the lowest precedence of all operators, the parentheses are necessary. 25

2. The sizeof Operator The sizeof is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be variable, a constant or a data type qualifier. Examples: m = sizeof ( sum ); n = sizeof ( long int ); k = sizeof ( 235L ); 26

Example 3.3 The different kinds of operators. #include<stdio.h> main() { int a, b, c, d; a = 15; b = 10; c = ++a - b; printf ("a=%d b=%d c=%d\n", a, b, c ); d = b++ +a; printf("a = %d b = %d d = %d\n", a, b, d ); printf("a/b = %d\n", a / b); printf("a%%b = %d\n", a%b ); printf("a *= b = %d\n", a *= b ); printf("%d\n", ( c>d )? 1 : 0 ); printf("%d\n", ( c<d )? 1 : 0 ); } 27

example3.4 variables in expressions and their evaluation #include<stdio.h> main() { float a, b, c, x, y, z; a = 9; b = 12; c = 3; x = a b / 3 + c * 2-1; y = a - b / ( 3+ c ) * ( 2-1 ); z = a - ( b / ( 3 + c ) * 2 ) - 1; printf ( "x = %f \n", x ); printf(" y = %f \n", y ) ; printf( "z = %f \n", z ) ; } 28

Some Computational Problems When expressions include real values, then it is important to take necessary precautions to guard against certain computational errors. For example, consider the following statements: a = 1.0 / 3.0; b = a * 3.0; There is no guarantee that the value of b will equal 1. Another problem is division by zero. The third problem is to avoid overflow and underflow errors. 29

example3.5 round-off errors in computation of floating point numbers. #include <stdio.h> main() { float sum, n, term; int count = 1; sum = 0; printf ( "Enter value of n \n " ) ; scanf ( "%f", &n ) ; term = 1.0 / n; while ( count <= n ) { sum = sum + term ; count++ ; } printf ( "Sum = %f \n", sum ) ; } 30

Type conversions in expressions 1. Implicit Type Conversion C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion. The rule of type conversion: the lower type is automatically converted to the higher type. 31

Type conversions in expressions for example, int i, x; float f; double d; long int li ; x = li / i + i * f d long long float float float float double int double The final result of an expression is converted to the type of the variable on the left of the assignment. 32

Type conversions in expressions The sequence of rules is given on page 67. Conversion Hierarchy is given below Conversion hierarchy double float unsigned long int long int int short char long double 33

Type conversions in expressions 2. Explicit conversion We can force a type conversion in a way. The general form of explicit conversion is ( type-name ) expression for example x = ( int ) 7.5 ; a = ( int ) 21.3 / (int) 4.5; a = ( float ) 3 / 2 ; a = float ( 3 / 2 ) ; 34

Operator precedence and Associativity Rules of Precedence and Associativity (1)Precedence rules decides the order in which different operators are applied. (2)Associativity rule decide the order in which multiple occurrences of the same level operator are applied. Table3.8 on page71 shows the summary of C Operators. for example, a = i +1== j k and 3!= x 35

Mathematical functions Mathematical functions such as cos, sqrt, log, etc. are frequently used in analysis of real-life problems. Table 3.9 on page 72 lists some standard math functions. All mathematical functions implement double type parameters and return double type values. To use any of mathematical functions, we should include the line: #include < math. h > 36