Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Operators Part II

 
Category: C++
Comments (0)

Operators in C++ Part II

In this C++ tutorial, you will learn about logical operators, && operator, || operator, conditional operator, comma operator, bitwise operator and sizeof() operator.



Logical Operators

The logical operators used are : !, &&, ||
The operator ! is called NOT operator. This has a single operand which reverses its value.


For example:


!true gives the value of false
!false gives the value of true


The operator && corresponds with Boolean logical operation AND. This operator returns the value of true if both its operands are true or if it returns false. The following table reflects the value of && operator:


&& Operator

x


y


x && y


true


true


true


true


false


false


false


true


false


false


false


false




 


 


 




The operator || corresponds with Boolean logical operation OR. The operator produces a true value if either one of its two operands are true and produces a false value only when both operands are false. The following table reflects the value of || operator:


|| Operator

x


y


x || y


true


true


true


true


false


true


false


true


true


false


false


false



Conditional Operator

The conditional operator evaluates an expression returning a value if that expression is true and a different value if the expression is evaluated as false.


The syntax is:



condition ? value1 : value2



For example:


In



7>5 ? x : y



Since 7 is greater than 5, true is returned and hence the value x is returned.


Comma Operator

This is denoted by, and it is used to separate two or more expressions.

For example:


exfor = (x=5, x+3);



Here value of 5 is assigned to x and then the value of x+3 is assigned to the variable exfor. Hence, value of the variable exfor is 8.

Bitwise Operators

The following are the bitwise operators available in C++:

  • & AND Bitwise AND
  • | OR Bitwise Inclusive OR
  • ^ XOR Bitwise Exclusive OR
  • ~ NOT Unary complement (bit inversion)
  • << SHL Shift Left
  • >> SHR Shift Right
  • Explicit type casting operator


sizeof() Operator

This operator accepts a single parameter and this can be a variable or data type. This operator returns the size in bytes of the variable or data type.


For example:



x = sizeof (char);



This returns the size of char in bytes. In this example, the size is 1 byte which is assigned to variable x.



Read Next: C++ Manipulators



 

 

Comments



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 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape