Difference between Post Increment (n++) and Pre Increment (++n)

Posted by Somesh Shinde On Tuesday, 28 June 2016 0 comments

Difference between Post Increment (n++) and Pre Increment (++n)

Post Increment (n++): It increases the value of variable by 1 after execution of the statement.
Pre Increment (++n): It increases the value of variable by 1 before execution of the statement.
ProgramOutput
class Demo
{
 public static void main(String args[])
 {  
  int n=10;
  System.out.println(n);
  System.out.println(n++);
  System.out.println(n);
 }
}
      
10
10
11
      

ProgramOutput
class Demo
{
 public static void main(String args[])
 {  
  int n=10;
  System.out.println(n);
  System.out.println(++n);
  System.out.println(n);
 }
}
      
10
11
11
      

0 comments:

Post a Comment