Our social:

Tuesday, August 16, 2016

How Increment and Decrement Operators in Java-Best Education Service

Increment and decrement operators in java

[Tutorial 8] :Increment and Decrement Operators in Java

If you remember the tutorial 3 , where I explained about the different Operators in Java , you would find that I have already given a basic introduction about the Increment and the Decrement Operators in Java. The main reason of creating a separate tutorial for this topic is that the questions related to Increment and Decrement Operators are very confusing and a good amount of practice is required in order to successfully solve the problems related to this concept.

What are Increment Operators in Java  ?

The increment operators are somewhat special kind of operators in java which are used to add the number ‘1’ to a particular digit whenever the increment operator in encountered.
The main syntax of Increment Operator is “++”. For example :-
a++ is an expression containing the increment operator. The meaning of this expression (a++) is  ( a = a+1 ) which means that 1 will be added to the value contained in the variable ‘a’
Let us consider an a program which will clear all doubts and queries 😉
public class increment_operators
{
           public static void main()
           {
                        int a = 10; 
                         a++; 
                         System.out.println(a);
         }
}
increment operator
program in Java

Output of the program
 output of the program

From the above example , we see that whenever “++” is used along with an integer , its value is increased by 1. “++” is an increment operator and increments the value of a number by 1.
These operators are generally used in an expression. For example :-
(a++) + (b++) – (++a) – (++b)

Types of Increment Operators :-

Well , the increment operators are also classified into two types and there are different rules regarding them which are often confused by programmers. These types determine the working of the increment operators.  The two types of Increment Operators are :-
  • Post-fix Increment Operator.
  • Pre-fix Increment Operator.

Post fix Increment Operator

The post-fix increment operators are in the form “a++” means they are used after the variable. The post-fix increment operator follows the general rule “First use then change” which means that the value of the integer which is given, is first used in the expression and then the effect of the postfix operator (increased by 1) is seen.
I will clear this when I have explained about the pre-fix operator also.

Pre-fix Increment Operator

The pre-fix increment operators are in the form “++a” means they are used before the variable. The post-fix increment operator follows the general rule “First change then use” which means that the value of the integer is first changed (increased by 1) and then it is used in the expression.
Now let us come to the working of these two types of increment operators 😀
Post-fix operators follow the rule “First use then change” . So , If in an expression , the post-fix operators are encountered , then for that time , the value of the integer will remain the same and the change will be seen thereafter.
Pre-fix operators follow the rule “First change then use” . So , If in an expression , the pre-fix operators are encountered , then the value of the integer will be changed at that time and then it will be used in the expression.
Let us see an example 😉
int a = 10;
int b = a++ + ++a  – a++ + a++ – ++a – ++a – a++ ;
System.out.println(b); 
What do you think will be the output of this one ? 😀
Lets calculate :)  ( The numbers in the strikethrough are the values which are the actual values but they will not be used at that point of time )
a = 10;
b = (a++  = 10
11 ) + (12
12 ) – (12
13 ) + (13
14 ) – (15
15 ) – (16
16  ) – (16
17) ;
b = 10 + 12 + – 12 + 13 – 15 – 16 – 16 ;
b = 35 – 59 ;
b = -24 ;
So , The final value of b will be -24 which will be printed as the output of the program.
Keep reading 😀 In the end of the program , I will post more questions and their answers like this 😀 but before that let us have some discussion about the decrement operators 😉

What are Decrement Operators in Java

The Decrement Opeartors are opposite to the increment operators. On one hand , The Increment Operators add ‘1’ to the number while on the other hand , decrement operators reduce the given integer by ‘1’ . The basic generic syntax of decrement operators in Java . Let us consider an example :-
a– is an expression containing the decrement operator. The meaning of this expression (a–) is  ( a = a-1 ) which means that 1 will be subtracted from the value contained in the variable ‘a’
Example 😀
public class decrement_operators
{
           public static void main()
           {
                        int a = 10; 
                         a–; 
                         System.out.println(a);
         }
}
The output of the program will be “9” . So you would have completely understood the working of the decrement operators in Java.

Types of Decrement Operators :-

Just as the Increment Operators are classified into two types , the decrement operators are also classified into two. The two types of decrement operators are :-
  • Pre-fix Decrement Operators.
  • Post-fix Decrement Operator.

Post fix Decrement Operator

The post-fix decrement operators are in the form “a–” means they are used after the variable. The post-fix decrement operator follows the general rule “First use then change” which means that the value of the integer which is given, is first used in the expression and then the effect of the postfix operator (decreased by 1) is seen.

Pre-fix Decrement Operator

The pre-fix decrement operators are in the form “–a” means they are used before the variable. The post-fix decrement operator follows the general rule “First change then use” which means that the value of the integer is first changed (decreased by 1) and then it is used in the expression.
You already know the working of the Post-fix and the Pre-fix Operators 😀

Illustrative Examples

  • int x = 5;
    int y = 10;
    int z = ++x +  y++ + x + y ;
    System.out.println(z);
The output of this program segment will be 33. Let us see how.
int z = 6 6 + 10
11 + 6
6  + 11
11
= 6 + 10 + 6 + 11
= 33
  •  int x =  5;
    int y = 10;
    int z = x++ + x + y++ + y;
    System.out.println(z);
The output of this will be 32.
int z = 5
6 + 6
6 + 10
11 + 11
11
= 5 + 6 + 10 + 11
= 32
  • int x = 5;
    int y = 10;
    int  z = ++x + ++y – –x + y– ;
    System.out.println(z);
The output will be 23
int z = 6
6 + 11 11 – 5 5 + 11
10
= 6 + 11 – 5 + 11
= 23
  • int x = 2;
    int y = 4;
    int c =  x++ + y++;
    int d = ++x + y;
    System.out.println(c);
    System.out.println(d);
Output :-
6
9
PS – How ? Find out yourselves.

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you