Operators and Expressions

Slides:



Advertisements
Similar presentations
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Chapter 2: Using Data.
OPERATORS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Operators & Expressions
Operators.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Prepared By: K. U. Khimani Assistant Professor IT Department.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CompSci 230 S Programming Techniques
C++ Programming Language Lecture 4 C++ Basics – Part II
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
Operators And Expressions
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
INSPIRING CREATIVE AND INNOVATIVE MINDS
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
Computing Fundamentals
University of Central Florida COP 3330 Object Oriented Programming
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
Introduction to Programming – 4 Operators
Chapter 3 Operators and Expressions
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
OPERATORS AND EXPRESSIONS IN C++
OPERATORS in C Programming
Operator King Saud University
DATA TYPES There are four basic data types associated with variables:
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

Operators and Expressions

Operators & Expressions C++ is very rich in built-in operators. In fact C++ places more significance on operators than most other computer languages do. C++ offers different classes of operators : arithmetic, relational, logical, increment-decrement, conditional, bitwise etc. In addition C++ has some special operators for particular tasks. Each operator performs a specific task it is designed for.

The Operations are represented by Operators The objects of the operations are referred to as Operands

Operators in C++ I/O Operators Arithmetic Operator Increment / Decrement Operators Relational Operator Logical Operator Conditional Operator ?:

I/O Operators Out put Operator(“<<“) Input Operator(“>>“) Used to direct a value to standard output. E.g: cout <<“The sum is 5”; Input Operator(“>>“) Used to read a value from standard input. E.g: int first, second; cin >>first>>second;

Arithmetic Operators Unary Operators Used to mean the value of a number. Requires only One Operand to act. Unary + Unary – Binary Operators Used for Arithmetic operations. Requires Two Operands to act.

Binary Operators Symbol Name (Action) Example Result Comment + Addition 6+5 11 Add values of its Two Operands - Subtraction 6-5 1 Subtracts values of its Two Operands (right operand from Left ) * Multiplication 6*5 30 Multiplies values of its Two Operands / Division 60/5 12 Divides value of Left Operand with that on Right % Modulus (or Remainder) 6/5 1 Gives the remainder when the value on the Left is divided by that on Right

Increment/Decrement Operators

How post fix & prefix works? int a = 10, C C = a++ + ++a + ++a + a cout<<C << a<< endl; OUTPUT 48 13

Relational Operators p q p<q p<=q p==q p>q p>=q p!=q 9 4 1 1 1 5 7 1 1 1 Relational Operators are the Operators used to compare between two values. It gives either True or False as answers. In the above Table column ‘p’ and ‘q’ represents values of p and q. While the other columns namely ‘p<q’, ‘p<=q’, ‘p==q’, ‘p>q’, ‘p>=q’ and ‘p!=q’ shows the answers produced when such comparisons have been carried out. Here ‘0’ represents ‘FALSE’ and ‘1’ represents ‘TRUE’.

Logical Operators Symbol Name Example Result Comment && AND (6 <=6)&&(5<3) False as One of it is False. || OR (6<=6)||(5<3) 1 One of the expression is true and hence True. ! NOT !(6<=6) Negates the result of the expression.

Conditional Operators ?: Stores a value depends upon a condition Operator ( ?: ) is known as ternary operator Form: expression1 ? Expression2:expression3

Eg To Illustrate Conditional Operators ?: int val, res, n = 1000; cin>>val; res = n + val > 1750 ? 400 : 200; cout<<res; i) if the input is 2000 iii) if the input is 500. OUTPUT i) 400, because the arithmetic operator + has higher precedence than ? : operator thus the condition before ? is taken as (n + val) and (1000+ 2000 )> 1750 is true. ii) 200, because (1000 + 500) > 1750 is false.

Some other operators Sizeof Comma

sizeof Operator Unary compile-time operator Returns the length (in bytes) of the variable or parenthesized type- specifier that it precedes Form sizeof var (where var is a declared variable) & sizeof (type) (where type is a C++ data type)

Comma Operator Used to string together several expressions. Example b = (a=3, a+1); 1st step : assigns a the value i.e., 3 2nd step : assigns b the value a+1 i.e., 4

Precedence of Operators ++ (post increment), --(post decrement) ++ (Pre increment), -- (pre decrement), - (unary minus), + (unary plus) * (multiply), / (divide), % (modulus) + (add), - (subtract) < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to) == (equal), != (not equal) && (logical AND) || (logical OR) ?: (conditional Operator) = (assignment), and other assignment operators. (Arithmetic) Comma Operator

Expressions Expressions Logical Arithmetic Expressions Expressions An Expression in C++ is any valid combinations of operators, constants and variables. Expressions Logical Expressions Arithmetic Expressions

Type Conversion C++ facilitates the following type conversion:- Process of converting one predefined type into another C++ facilitates the following type conversion:- Implicit type conversion: An implicit type conversion is a conversion that is automatically performed by the compiler without the programmer’s intervention. Explicit type conversion: An explicit type conversion is a user-defined one or that is a forced conversion.

Implicit type Conversion step no. if either’s type is Then resultant type of other operand otherwise 1 long double long double step 2 2 double double step 3 3 float float step 4 4 --- integral promotion takes place followed by step 5 --- 5 unsigned long unsigned long step 6 6 long int and the other is unsigned int long int (provided long int can represent all values of unsigned int) unsigned long int (if all values of unsigned int can’t be represented by long int) step 7 7 long long step 8 8 unsigned unsigned both operands -int

Some valid Logical Expressions x>y (y+z) >+ (x/z) (a+b) > c && (c +d )>a (x) x || y && z (y > x) || (z<y) (-y) (x-y) (x > y) & & (!y <z)

C++ Shorthands Form var = var operator expression is same as

Examples of C++ Shorthands equivalent to x = x -10; x * = 3; equivalent to x = x * 3; x/ = 2; equivalent to x = x / 2; X % = z; equivalent to x = x % z;