SlideShare a Scribd company logo
Lecture 2 : C++
VARIABLE SCOPE, OPERATORS IN C++
What is the Scope of A variable ?
Scope refers to the visibility of variables. In other
words, which parts of your program can see or use
it. Normally, every variable has a global scope.
Once defined, every part of your program can
access a variable.
A scope is a region of the program and broadly speaking
there are three places, where variables can be declared:
 Inside a function or a block which is called local variables,
 In the definition of function parameters which is called formal
parameters.
 Outside of all functions which is called global variables.
Local Variables
 Variables that are declared inside a function or block are local
variables. They can be used only by statements that are inside that
function or block of code. Local variables are not known to
functions outside their own. Following is the example using local
variables
int main()
{
//local variable declaration
int a , b;
int c ;
// actual initialization
a = 10;
b = 20;
c = a + b;
count <<c;
return 0;
}
Global Variable
 Global variables are defined outside of all the functions, usually on
top of the program. The global variables will hold their value
throughout the life-time of your program.
 A global variable can be accessed by any function. That is, a global
variable is available for use throughout your entire program after its
declaration.
#include <iostream>
int g;
int main()
{
//local variable declaration
int a ,b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout <<g;
return 0;
}
#include <iostream>
Int g = 20;
Int main()
{
//local variable declaration
Int g = 10;
Cout <<g;
Return 0;
}
Lecture 2 C++ | Variable Scope, Operators in c++
Operators in C++
An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations. C++ is rich
in built-in operators and provides the following types of
operators:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
Arithmetic Operators:
Assume variable A holds 10 and variable B holds 20
Operator Description Result
+ Adds two operands 30
- Subtracts second operand from first -10
* Multiplies both Operands 200
/ Divides numerator by De-numerator 2
% Modulus Operator and reminder of after integer
division
0
++ Increment operator , increases integer value by one 11
-- Decrement Operator, Decreases Integer Value by One 9
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;
}
Relational Operators
Assume variable A holds 10 and variable B holds 20
Operator Description Example
== Checks if the Value of two Operands are Equal or
not , if yes then condition becomes True
(A==B) is not true
!= Checks if the value of two operands are equal or
not , if values are not equal then condition
becomes true
(A!=B) is true
> Checks if the value of Left operand is greater than
the value of right operand , if yes then condition
becomes true,
(A>B) is not true
Relational Operators Contd.
Assume variable A holds 10 and variable B holds 20
Operator Description Example
< Checks if the Value of Left Operand is less than
the value of right Operand , If yes then
condition becomes True.
(A<B) is true.
>= Checks if the value of left operand is greater
than or equal to the value of right Operand, If
yes then condition becomes True
(A>=B) is not True
<= Checks if the value of the left Operand is less
than or equal to the value of Right Operand. If
yes then condition becomes true.
(A<=B) is true
#include <iostream>
using namespace std;
main()
{
int a = 21;
int b = 10;
int c ;
if( a == b )
{
cout << "Line 1 - a is equal to b" << endl ;
}
else
{
cout << "Line 1 - a is not equal to b" << endl ;
}
if ( a < b )
{
cout << "Line 2 - a is less than b" << endl ;
}
else
{
cout << "Line 2 - a is not less than b" << endl ;
}
if ( a > b )
{
cout << "Line 3 - a is greater than b" << endl ;
}
else
{
cout << "Line 3 - a is not greater than b" << endl ;
}
/* Let's change the values of a and b */
a = 5;
b = 20;
if ( a <= b )
{
cout << "Line 4 - a is either less than 
or euqal to b" << endl ;
}
if ( b >= a )
{
cout << "Line 5 - b is either greater than 
or equal to b" << endl ;
}
return 0;
}
Logical Operators
Operator Descripton Example
&& Called Logical And Operator. If Both the
operands are non Zero , then Condition
becomes true.
(A && B) is false
|| Called Logical OR Operator. If any of the two
Operands is non Zero, Then Condition
becomes True.
A||B is true
! Called Logical NOT Operator. Use to Reverse
the logical State of its Operand. If condition is
true, Then Logical NOT Operator will make
False.
!(A && B) is true
main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
cout << "Line 1 - Condition is true"<< endl ;
}
if ( a || b )
{
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */
a = 0;
b = 10;
if ( a && b )
{
cout << "Line 3 - Condition is true"<< endl ;
}
else
{
cout << "Line 4 - Condition is not true"<< endl ;
}
if ( !(a && b) )
{
cout << "Line 5 - Condition is true"<< endl ;
}
return 0;
}
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation.
P Q P & Q P|Q P^Q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
if A = 60; and B = 13
Assume if A = 60; and B = 13; now in binary format they
will be as follows:
 A = 0011 1100
 B = 0000 1101
 A&B = 0000 1100
 A|B = 0011 1101
 A^B = 0011 0001
 ~A = 1100 0011
Operator Description Result
& Binary AND Operator copies a bit to the
result if it exists in both operands.
(A & B) will give 12 which is
0000 1100
| Binary OR Operator copies a bit if it exists
in either operand.
(A | B) will give 61 which is
0011 1101
^ Binary XOR Operator copies the bit if it is
set in one operand but not both.
(A ^ B) will give 49 which is
0011 0001
Operator Description Result
~ Binary Ones Complement Operator is
unary and has the effect of 'flipping' bits.
(~A ) will give -61 which is
1100 0011 in 2's
complement form due to a
signed binary number.
<< Binary Left Shift Operator. The left operands
value is moved left by the number of bits
specified by the right operand.
A << 2 will give 240 which is
1111 0000
>> Binary Right Shift Operator. The left
operands value is moved right by the
number of bits specified by the right
operand.
A >> 2 will give 15 which is
0000 1111
main()
{
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a & b; // 12 = 0000 1100
cout << "Line 1 - Value of c is : " << c << endl ;
c = a | b; // 61 = 0011 1101
cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - Value of c is: " << c << endl ;
c = ~a; // -61 = 1100 0011
cout << "Line 4 - Value of c is: " << c << endl ;
c = a << 2; // 240 = 1111 0000
cout << "Line 5 - Value of c is: " << c << endl ;
c = a >> 2; // 15 = 0000 1111
cout << "Line 6 - Value of c is: " << c << endl ;
return 0;
}
Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns
values from right side operands to left side
operand
C = A + B will assign
value of A + B into C
+= Add AND assignment operator, It adds right
operand to the left operand and assign the
result to left operand
C += A is equivalent to
C = C + A
-= Subtract AND assignment operator, It
subtracts right operand from the left
operand and assign the result to left
operand
C -= A is equivalent to
C = C - A
Assignment Operators
Operator Description Example
*= Multiply AND assignment operator, It
multiplies right operand with the left
operand and assign the result to left
operand
C *= A is equivalent to
C = C * A
/= Divide AND assignment operator, It divides
left operand with the right operand and
assign the result to left operand
C /= A is equivalent to
C = C / A
%= Modulus AND assignment operator, It takes
modulus using two operands and assign the
result to left operand
C %= A is equivalent to
C = C % A
Assignment Operators
Operator Description Example
<<= Left shift AND assignment operator C<<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment
operator
C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment
operator
C |= 2 is same as C = C | 2
main()
{
int a = 21;
int c ;
c = a;
cout << "Line 1 - = Operator, Value of c = : " <<c<< endl ;
c += a;
cout << "Line 2 - += Operator, Value of c = : " <<c<< endl ;
c -= a;
cout << "Line 3 - -= Operator, Value of c = : " <<c<< endl ;
c *= a;
cout << "Line 4 - *= Operator, Value of c = : " <<c<< endl ;
c /= a;
cout << "Line 5 - /= Operator, Value of c = : " <<c<< endl ;
c = 200;
c %= a;
cout << "Line 6 - %= Operator, Value of c = : " <<c<< endl ;
c <<= 2;
cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl
;
c >>= 2;
cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl
;
c &= 2;
cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ;
c ^= 2;
cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ;
c |= 2;
cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ;
return 0;
}
Any Queries ?
Thank you ;)

More Related Content

PPT
Enumerated data types in C
PPTX
Data types in c++
PPTX
Control structures in c++
PPTX
Programming in C Presentation upto FILE
PPTX
Constructor and Types of Constructors
PPTX
Object oriented programming
PPTX
Library functions in c++
PDF
Unit ii chapter 2 Decision making and Branching in C
Enumerated data types in C
Data types in c++
Control structures in c++
Programming in C Presentation upto FILE
Constructor and Types of Constructors
Object oriented programming
Library functions in c++
Unit ii chapter 2 Decision making and Branching in C

What's hot (20)

PPTX
Control statements in java
PPTX
Exception handling
PDF
Function overloading ppt
PPSX
C++ Programming Language
PPTX
Exception handling c++
PPTX
Templates in C++
PPTX
C++ decision making
PPTX
Virtual base class
PPTX
Decision Making and Looping
PPTX
Inheritance in java
PDF
Operator overloading C++
PPT
Introduction to programming using Visual Basic 6
PPTX
Types of Constructor in C++
PPTX
Encapsulation C++
PPTX
virtual function
PPTX
Basic Data Types in C++
PPSX
Data Types & Variables in JAVA
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
History Of JAVA
PPT
JavaScript Control Statements I
Control statements in java
Exception handling
Function overloading ppt
C++ Programming Language
Exception handling c++
Templates in C++
C++ decision making
Virtual base class
Decision Making and Looping
Inheritance in java
Operator overloading C++
Introduction to programming using Visual Basic 6
Types of Constructor in C++
Encapsulation C++
virtual function
Basic Data Types in C++
Data Types & Variables in JAVA
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
History Of JAVA
JavaScript Control Statements I
Ad

Viewers also liked (20)

PPT
Operators in C++
PPT
Operators and Expressions in C++
PPT
Operator & Expression in c++
PPTX
Concept Of C++ Data Types
 
PPT
Lecture 3
PPTX
PDF
Chapter 5 - Operators in C++
PPTX
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
PPTX
Variables and data types in C++
PPTX
A presentation on types of network
PPTX
Overloading of io stream operators
PPTX
03. operators and-expressions
PPT
Operation and expression in c++
PDF
C++ L01-Variables
PPTX
PPT
CBSE Class XI :- Operators in C++
PPTX
Variables in C and C++ Language
PPTX
Operators
PPTX
Types of computer networks
PPT
Basic structure of C++ program
Operators in C++
Operators and Expressions in C++
Operator & Expression in c++
Concept Of C++ Data Types
 
Lecture 3
Chapter 5 - Operators in C++
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Variables and data types in C++
A presentation on types of network
Overloading of io stream operators
03. operators and-expressions
Operation and expression in c++
C++ L01-Variables
CBSE Class XI :- Operators in C++
Variables in C and C++ Language
Operators
Types of computer networks
Basic structure of C++ program
Ad

Similar to Lecture 2 C++ | Variable Scope, Operators in c++ (20)

PPT
C++ chapter 2
PPTX
Session03 operators
PPTX
11operator in c#
PDF
Programming C Part 02
PPTX
C operators
PDF
Constructor and destructors
PPTX
Python operators part2
PPTX
Computer programming 2 Lesson 7
DOCX
Bit shift operators
PPTX
btwggggggggggggggggggggggggggggggisop correct (1).pptx
PDF
itft-Operators in java
PDF
Java basic operators
PDF
Java basic operators
PPTX
C Operators and Control Structures.pptx
DOCX
Programming Fundamentals lecture 7
PDF
Python : basic operators
PPTX
Operators and it's type
PPT
python operators.ppt
PDF
C# Fundamentals - Basics of OOPS - Part 2
C++ chapter 2
Session03 operators
11operator in c#
Programming C Part 02
C operators
Constructor and destructors
Python operators part2
Computer programming 2 Lesson 7
Bit shift operators
btwggggggggggggggggggggggggggggggisop correct (1).pptx
itft-Operators in java
Java basic operators
Java basic operators
C Operators and Control Structures.pptx
Programming Fundamentals lecture 7
Python : basic operators
Operators and it's type
python operators.ppt
C# Fundamentals - Basics of OOPS - Part 2

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Teaching material agriculture food technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Teaching material agriculture food technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
MIND Revenue Release Quarter 2 2025 Press Release
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Per capita expenditure prediction using model stacking based on satellite ima...

Lecture 2 C++ | Variable Scope, Operators in c++

  • 1. Lecture 2 : C++ VARIABLE SCOPE, OPERATORS IN C++
  • 2. What is the Scope of A variable ? Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. Normally, every variable has a global scope. Once defined, every part of your program can access a variable.
  • 3. A scope is a region of the program and broadly speaking there are three places, where variables can be declared:  Inside a function or a block which is called local variables,  In the definition of function parameters which is called formal parameters.  Outside of all functions which is called global variables.
  • 4. Local Variables  Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables
  • 5. int main() { //local variable declaration int a , b; int c ; // actual initialization a = 10; b = 20; c = a + b; count <<c; return 0; }
  • 6. Global Variable  Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.  A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.
  • 7. #include <iostream> int g; int main() { //local variable declaration int a ,b; // actual initialization a = 10; b = 20; g = a + b; cout <<g; return 0; }
  • 8. #include <iostream> Int g = 20; Int main() { //local variable declaration Int g = 10; Cout <<g; Return 0; }
  • 10. Operators in C++ An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators:  Arithmetic Operators  Relational Operators  Logical Operators  Bitwise Operators  Assignment Operators  Misc Operators
  • 11. Arithmetic Operators: Assume variable A holds 10 and variable B holds 20 Operator Description Result + Adds two operands 30 - Subtracts second operand from first -10 * Multiplies both Operands 200 / Divides numerator by De-numerator 2 % Modulus Operator and reminder of after integer division 0 ++ Increment operator , increases integer value by one 11 -- Decrement Operator, Decreases Integer Value by One 9
  • 12. main() { int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl ; c = a * b; cout << "Line 3 - Value of c is :" << c << endl ; c = a / b; cout << "Line 4 - Value of c is :" << c << endl ; c = a % b; cout << "Line 5 - Value of c is :" << c << endl ; c = a++; cout << "Line 6 - Value of c is :" << c << endl ; c = a--; cout << "Line 7 - Value of c is :" << c << endl ; return 0; }
  • 13. Relational Operators Assume variable A holds 10 and variable B holds 20 Operator Description Example == Checks if the Value of two Operands are Equal or not , if yes then condition becomes True (A==B) is not true != Checks if the value of two operands are equal or not , if values are not equal then condition becomes true (A!=B) is true > Checks if the value of Left operand is greater than the value of right operand , if yes then condition becomes true, (A>B) is not true
  • 14. Relational Operators Contd. Assume variable A holds 10 and variable B holds 20 Operator Description Example < Checks if the Value of Left Operand is less than the value of right Operand , If yes then condition becomes True. (A<B) is true. >= Checks if the value of left operand is greater than or equal to the value of right Operand, If yes then condition becomes True (A>=B) is not True <= Checks if the value of the left Operand is less than or equal to the value of Right Operand. If yes then condition becomes true. (A<=B) is true
  • 15. #include <iostream> using namespace std; main() { int a = 21; int b = 10; int c ; if( a == b ) { cout << "Line 1 - a is equal to b" << endl ; } else { cout << "Line 1 - a is not equal to b" << endl ; } if ( a < b ) { cout << "Line 2 - a is less than b" << endl ; } else { cout << "Line 2 - a is not less than b" << endl ; } if ( a > b ) { cout << "Line 3 - a is greater than b" << endl ; } else { cout << "Line 3 - a is not greater than b" << endl ; } /* Let's change the values of a and b */ a = 5; b = 20; if ( a <= b ) { cout << "Line 4 - a is either less than or euqal to b" << endl ; } if ( b >= a ) { cout << "Line 5 - b is either greater than or equal to b" << endl ; } return 0; }
  • 16. Logical Operators Operator Descripton Example && Called Logical And Operator. If Both the operands are non Zero , then Condition becomes true. (A && B) is false || Called Logical OR Operator. If any of the two Operands is non Zero, Then Condition becomes True. A||B is true ! Called Logical NOT Operator. Use to Reverse the logical State of its Operand. If condition is true, Then Logical NOT Operator will make False. !(A && B) is true
  • 17. main() { int a = 5; int b = 20; int c ; if ( a && b ) { cout << "Line 1 - Condition is true"<< endl ; } if ( a || b ) { cout << "Line 2 - Condition is true"<< endl ; } /* Let's change the values of a and b */ a = 0; b = 10; if ( a && b ) { cout << "Line 3 - Condition is true"<< endl ; } else { cout << "Line 4 - Condition is not true"<< endl ; } if ( !(a && b) ) { cout << "Line 5 - Condition is true"<< endl ; } return 0; }
  • 18. Bitwise Operators Bitwise operator works on bits and perform bit-by-bit operation. P Q P & Q P|Q P^Q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1
  • 19. if A = 60; and B = 13 Assume if A = 60; and B = 13; now in binary format they will be as follows:  A = 0011 1100  B = 0000 1101  A&B = 0000 1100  A|B = 0011 1101  A^B = 0011 0001  ~A = 1100 0011
  • 20. Operator Description Result & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
  • 21. Operator Description Result ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111
  • 22. main() { unsigned int a = 60; // 60 = 0011 1100 unsigned int b = 13; // 13 = 0000 1101 int c = 0; c = a & b; // 12 = 0000 1100 cout << "Line 1 - Value of c is : " << c << endl ; c = a | b; // 61 = 0011 1101 cout << "Line 2 - Value of c is: " << c << endl ; c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - Value of c is: " << c << endl ; c = ~a; // -61 = 1100 0011 cout << "Line 4 - Value of c is: " << c << endl ; c = a << 2; // 240 = 1111 0000 cout << "Line 5 - Value of c is: " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - Value of c is: " << c << endl ; return 0; }
  • 23. Assignment Operators Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
  • 24. Assignment Operators Operator Description Example *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
  • 25. Assignment Operators Operator Description Example <<= Left shift AND assignment operator C<<= 2 is same as C = C << 2 >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2 |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
  • 26. main() { int a = 21; int c ; c = a; cout << "Line 1 - = Operator, Value of c = : " <<c<< endl ; c += a; cout << "Line 2 - += Operator, Value of c = : " <<c<< endl ; c -= a; cout << "Line 3 - -= Operator, Value of c = : " <<c<< endl ; c *= a; cout << "Line 4 - *= Operator, Value of c = : " <<c<< endl ; c /= a; cout << "Line 5 - /= Operator, Value of c = : " <<c<< endl ; c = 200; c %= a; cout << "Line 6 - %= Operator, Value of c = : " <<c<< endl ; c <<= 2; cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl ; c >>= 2; cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl ; c &= 2; cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ; c ^= 2; cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ; c |= 2; cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ; return 0; }