Advertisement
476179

IncrementAndDecrementOperators- loops1

Nov 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. package lessons;
  2.  
  3. public class IncrementAndDecremementOp
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. //just add a value to a current value
  9. //a 'Postfix' operator because it comes after the variable
  10. //prefix & postfix op example:
  11.  
  12. int num = 4;
  13.  
  14.  
  15. //increment
  16. System.out.println("num is "+num);
  17.  
  18. num ++;
  19. //means +1 to current value of num, now it is s5
  20.  
  21. System.out.println(" num is now"+num);//num is 5
  22.  
  23. //decrement
  24. num --;
  25. // means -1 to current value of num, now it is 4
  26.  
  27. //a 'Postfix' operator because it comes after the variable
  28. //prefix & postfix op example:
  29. //postfix
  30. System.out.println("println first"+num++);// 5
  31. System.out.println("after println:"+num);//4
  32. //adds after it is printed not before
  33.  
  34. //prefix
  35. System.out.println("println first "+ ++num);//5
  36. System.out.println(num);//5
  37. //adds one before printing
  38.  
  39.  
  40.  
  41. int x = 5;
  42. int y = ++x+x--;
  43. // adds 1 to 5, so ++x = 6, now +x and x is 6 so it is 12, ?????
  44. System.out.println(y);
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement