Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Operators Part I

 
Category: C++
Comments (1)

C++ Operators Part I

In this C++ tutorial, you will learn about operators, assignment operator, arithmetic operators, compound assignment operators, increment and decrement operator, the functionality of prefix and postfix operators, relational and equality operators.



The operators available in C++ programming language are:


  • Assignment Operator denoted by =
  • Arithmetic operators denoted by +, -, *, /, %
  • Compound assignment Operators denoted by +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=
  • Increment and Decrement operator denoted by ++, --
  • Relational and equality operators denoted by ==, !=, >, <, >=, <=
  • Logical operators denoted by !, &&, ||
  • Conditional operator denoted by ?
  • Comma operator denoted by ,
  • Bitwise Operators denoted by &, |, ^, ~, <<, >>
  • Explicit type casting operator
  • sizeof()

Assignment Operator

This is denoted by symbol =. This operator is used for assigning a value to a variable. The left of the assignation operator is known as the lvalue (left value), which must be a variable. The right of the assignation operator is known as the rvalue (right value). The rvalue can be a constant, a variable, the result of an operation or any combination of these.


For example:


x = 5;


By following the right to left rule the value 5 is assigned to the variable x in the above assignment statement.


Arithmetic operators

The operators used for arithmetic operation sin C++ are:


  • + For addition
  • - For subtraction
  • * For multiplication
  • / For division
  • % For modulo

Compound assignment Operators

This operator is used when a programmer wants to update a current value by performing operation on the current value of the variable.


For example:


Old += new is equal to
Old = old + new


Compound assignment operators function in a similar way the other operators +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= function.


Increment and Decrement Operator

The increment operator is denoted by ++ and the decrement operator by --. The function of the increment operator is to increase the value and the decrement operator is to decrease the value. These operators may be used as either prefix or postfix. A Prefix operator is written before the variable as ++a or –a. A Postfix operator is written after the variable as a++ or a--.


The Functionality of Prefix and Postfix Operators

In the case that the increment or decrement operator is used as a prefix ( ++a or –a), then the value is respectively increased or decreased before the result of the expression is evaluated. Therefore, the increased or decreased value, respectively, is considered in the outer expression. In the case that the increment or decrement operator is used as a postfix (a++ or a--), then the value stored in a is respectively increased or decreased after being evaluated. Therefore, the value stored before the increase or decrease operation is evaluated in the outer expression.


For Example:



y=3;
x=++y;            
 //Prefix : Here Value of x becomes 4


But for the postfix operator namely as below:

y=3                              //Postfix : Here Value of x is 3 and Value of y is  4 
x=y++;



Relational and Equality Operators

These operators are used for evaluating a comparison between two expressions. The value returned by the relational operation is a Boolean value (true or false value).
The operators used for this purpose in C++ are:


  • == Equal to
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Read Next: C++ Operators Part II



 

 

Comments


parvati said:

  good,one can easily recollect all the points,thanks
November 15, 2008, 9:06 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape