Page 28 - Modular_V1.1_Flipbook
P. 28
–= Subtraction assignment It subtracts right operand from the left x –= 3
operand and assigns the result to left
operand. x–=3 is equivalent to x=x–3.
*= Multiplication It multiplies right operand with the left x *= 3
assignment operand and assigns the result to left
operand. x*=3 is equivalent to x=x*3.
/= Division assignment It divides left operand with the right x /= 3
operand and assigns the result to left
operand. x/=3 is equivalent to x=x/3.
%= Remainder assignment It takes modulus of two operands and x %= 3
assigns the result to left operand. x%=3 is
equivalent to x=x%3.
Program 5: To use the assignment operators.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a = 5, b = 7, c = 2, d = 10;
a += 2;
cout<<”The value of a is: “<< a <<”\n”;
b -= 1;
cout<<”The value of b is: “<< b <<”\n”;
c *= a;
cout<<”The value of c is: “<< c <<”\n”;
d %= 3;
cout<<”The value of d is: “<< d <<”\n”;
getch();
}
Increment and Decrement Operators
C++ provides increment (++) and decrement (– –) operators which are used to increase or
decrease the value of a variable by 1. These are called unary operators as they need only one
operator to work. For example:
a = 10;
a = ++a; // means a = a + 1
a = --a; // means a = a - 1
C++ allows you to use these operators in two ways: prefix and postfix. The (++ and – –) operators
are called “prefix” if they used before the operand and postfix if they used after the operand.
26 Touchpad MODULAR (Version 1.1)-X

