INCREMENTAL OPERATOR
Incremental Operator:-
- We will used increment operator to increment the value by one.
- There are two types of increment operators we have.
- Pre-increment.
- Post-increment.
- Pre-increment:
- Rules:-
- Increment the value by one.
- Update the incremented value in the same variable.
- Any operation is going on we will used the incremented value.
- class Calculator
- {
- public static void main(String [] args)
- {
- int n= 35;
- int b= 21;
- ++n;
- ++b;
- System.out.println(b);
- System.out.println(n);
- }
- }
Output:
22
36
36
- class Calculator
- {
- public static void main(String [] args)
- {
- int n= 35;
- int b= 21;
- ++n;
- ++n;
- System.out.println(b);
- System.out.println(n);
- }
- }
Output:
21
37
37
- class Calculator
- {
- public static void main(String [] args)
- {
- int n= 35;
- int b= 21;
- int c= ++b;
- System.out.println(b);
- System.out.println(n);
- System.out.println(c);
- }
- }
Output:
35
22
22
22
22
- class Calculator
- {
- public static void main(String [] args)
- {
- int b= 21;
- int c= ++b;
- System.out.println(b);
- System.out.println(c);
- }
- }
- class Calculator
- {
- public static void main(String [] args)
- {
- int b= 21;
- int c= ++b;
- ++b;
- System.out.println(b);
- System.out.println(c);
- }
- }
- {
- public static void main(String [] args)
- {
- int b= 21;
- int c= ++b;
- int a= ++c;
- System.out.println(a);
- System.out.println(c);
- System.out.println(b);
- }
- }
- class Calculator
- {
- public static void main(String [] args)
- {
- int b= 21;
- int c= ++b;
- int a= ++b;
- int d= ++a;
- System.out.println(a);
- System.out.println(c);
- System.out.println(b);
- System.out.println(d);
- }
- }
- Post-increment:
- Rules:-
- Increment the value by one.
- Update the incremented value in the same variable.
- Any operation is going on we will used the current value but not the incremented value.
- class Calculator
- {
- public static void main(String [] args)
- {
- int b= 21;
- int c= b++;
- System.out.println(c);
- System.out.println(b);
- }
- }
- class Calculator
- {
- public static void main(String [] args)
- {
- int a= 21;
- int b= a++;
- System.out.println(a);
- System.out.println(b);
- }
- }
- class Calculator
- {
- public static void main(String [] args)
- {
- int a= 21;
- int b= a++;
- int c= b++;
- System.out.println(a);
- System.out.println(b);
- System.out.println(c);
- }
- }
Output:
22
22
21
22
21
- class Calculator
- {
- public static void main(String [] args)
- {
- int a= 21;
- int b= a++;
- int c= ++b;
- int d= c++;
- int e= ++d;
- int f= e++;
- System.out.println(a);
- System.out.println(b);
- System.out.println(c);
- System.out.println(d);
- System.out.println(e);
- System.out.println(f);
- }
- }
0 Comments