Suppose we have written a method
int printhello()
{
cout<<"Say Hello!!!";
return 0;
}
coz we have specified the return type for the method "int" in this case. then we have to return some value from the function to the Callee function.
Thats why we have to write this.
The alternative of this is, in this case, just specify return type as void instead of int.
Like this one:
void printhello()
{
cout<<"Say Hello!!!";
}
Operators are used to compute and compare values and test any condition. Operators can be classified as:
1. Arithmetic Operators.
2. Arithmetic Assignment Operators.
3. Unary Operators.
4. Comparison Operators.
5. Logical Operators.
6. Conditional Operators.
Now We'll take Arithmetic Operators first.
As the name suggests we use Arithmetic Operators to perform ny Arithmetic Operations such as Addition, Subtraction, Multiplication and Deletion. So, these are the Arithmetic Operators : (+, -, *, /, %)
Examples:
x=y+z
Adds the value of y and z and stores the result in x
x=y-z
Subtracts the value of z from y and stores the result in x
x=y*z
Multiplies the value of y and z and stores the result in x
x=y/z
Divides the y by z and stores the result in x
x=y%z
Divides the y by z and stores the remainder in x.
In the above operators only %(Modulo Operator) needs attention. This is used to find the remainder of a division expression.
*****-----------------------------------
Now Take the Arithmetic Assignment Operators
Take any operator, like for example, += operator adds the value of the variable on the right side to the value of variable on the left side and stores the result on d variable at left side.
For example:
X += Y
This is equivalent to (X=X+Y). Both Statements will produce d same result
Examples:
x+=y
Adds the value of x and y and stores the result in x
x-=y
Subtracts the value of y from x and stores the result in x
x*=y
Multiplies the value of x and y and stores the result in x
x/=y
Divides the x by y and stores the result in x
x%=y
Divides the x by y and stores the remainder in x.
Unary operators are very useful operators in scenarios where we want to increment/Decrement the value of a variable by 1.
There are two unary operators:
++ (Two Plus Signs)
- - (Two Minus Signs)
Now Lets use them,
Suppose we want to increment the value of x by 1. We can do this by two methods.
X=X+1;
or
X++;
Yes, In Second Statement i've used unary operator to increment the value of x by one.
You can choose any method which ever suits you.
Same as you can use the decrement unary operator.
i.e.
X--;
Which will decrement the value of x by 1.
Now, In Unary Operators one thing is interesting is that, you can use it as Prefix and Postfix Notations. Like:
Suppose we have declared two variables X and Y and assigned 10 to them,
int x=y=10;
X=Y++;
and
X=++Y;
Both Statements will increment the value of Y by 1. Then what's the diff. between these two statements. The difference is that,
In 1st statement Firstly the value of Y will be copied in X and then Y will be incremented. Means here value of X and Y will be
X will show 10, and
Y will show 11.
But in Second Statement they both will show 11. Because In Second statement Y will first be incremented and then be assigned to X. So both will Show 11.