Compound Assignment Statement
An Assignment Statement That is used to assign the same value to multiple variables is called compound assignment statement. The compound assignment statement is also called multiple assignment statement.For example, to assign a value 18 to variables a , b and c the compound assignment statement is written as;
a = b = c = 20 ;
Compound Assignment Operator
C / C++ language provides compound assignment operator. These operators provide shortcut ways to add, subtract, or divide value to or divide values to or from variables. Each command assignment operator is formed by combining an arithmetic operator and assignment operator. In C, the commonly used compound assignment operator as +=, -=, *=, /=, and %=.
Simple Assignment
Statement
|
Compound Assignment
Statement
|
x = x + 2 ;
x = x - 8 ;
x = x * 12 ;
x = x / 4 ;
x = x % 7 ;
|
x + = 2 ;
x - = 8 ;
x * = 12 ;
x / = 4 ;
x % = 7 ;
|
Example in C
#include < stdio.h >
#include < conio.h >
void main ( )
{
int a , b , c , prod ;
a = b = c = 20 ;
printf ( " Product = %d " , prod ) ;
}
Example in C++
#include < iostream >
using namespace std;
int main ( )
{
int a , b , c , prod ;
a = b = c = 20 ;prod = a * b * c ;
cout << " Product = " << prod ;
return 0;
}
Output of the program
Product
= 8000
|
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.