Operators based on operand count – Computer Sir Ki Class

Code Learning #CPP#1728

## Operators based on operand count

Binary, unary and ternary operator classification is based on number of operands.

## Learning Objectives

• Understanding operators classification on the basis of number of operands.

## Source Code

 #include using namespace std; int main() { int a=10; //Binary Operators cout<

## Run Output

``````20
0
100
1
10
11
10
11``````

## Code Understanding

int a=10;
This will initialise the integer variable a with a value of 10.

cout<<a+a<<endl; cout<<a-a<<endl; cout<<a*a<<endl; cout<<a/a<<endl;
Will produce output 20, 0, 100 and 1 as per the given arithmetic operator.

cout<<a++<<endl; cout<<a<<endl;
This will post increment the value of a, which means that the effect of increment will be visible in next instruction. So the output of 10 and 11 will be produced by these two instructions.

cout<<–a<<endl;
This will pre-decrement the value of a. The value in previous instruction is 11 so this will now print 10 again.

int b=(a)?++a:–a;
This is the ternary operator which is a combination of three operands.  (a) means the condition check operand. Here we are checking if a is true. Since a is non-zero value so it is taken as true and hence the first operand before : character is activated. If condition is false then second value after : character is activated. So b will contain ++a which is 11. Here three operands are (Condition check operand) ? (Value when true operand) : (value when false operand)

cout<<b<<endl;
The value of b which is 11 shall be printed.

Suggested Filename(s): operators-but.cpp,operandcount.cpp

CSKC| Created: 15-Dec-2017 | Updated: 4-Jan-2019|

×