Page 261 - Computer_Science_F5
P. 261
Computer Science The following Program Example 4.16 demonstrates the use of compound operators.
The output is shown in Figure 4.32.
Program Example 4.16:
FOR ONLINE READING ONLY
Compound assignment operators
#include<iostream>
using namespace std;
int main()
{
int x, y = 5;
x = y;
x+=8;
cout<<”The value of x is “<<x<<endl;
return 0;
}
Output:
Figure 4.32: Output of the program
(d) Increment and decrement operators
Increment (++) and decrement (--) operators are unary operators that can add or
subtract one to the variable. The increment operators add one to the variable while
decrements subtract one. The increments and decrements operators can be written
before variable as prefixes and after a variable as the suffixes. The increment and
decrement operators are mainly used to iterate integers and floating data types. Table
4.9 shows increment and decrement operators and their meaning.
Table 4.9: Increment and decrement operators
Operator Meaning Explanation
++i Pre increment Increment by 1 and then conduct operation
- -i Pre decrement Decrement by 1 and then conduct operation
i++ Post increment Conduct operation and then increment by 1
i-- Post decrement Conduct operation and then decrement by 1
252
for Advanced Secondary Schools
Computer Science Form 5.indd 252 23/07/2024 12:33

