Thursday 25 July 2019

Processing: Arithmetic Operators: ++, --


++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1

Pre increment
    Syntax:
    ++variable

 Pre increment operator increments the variable value by 1 immediately.
  
Operators.pde
int var = 10;
println(++var);

Output
11

Post increment
    Syntax:
        variable++

post increment operator increments the variable value by 1 after executing the current statement.


Operators.pde
int var = 10;

println(var++);
println(var);

Output
10
11

Pre Decrement
    Syntax:
        --variable

 Pre decrement operator decrements the variable value by 1 immediately.
   

Operators.pde
int var = 10;
println(--var);

Output
9

Post Decrement
    Syntax:
        variable--

post decrement operator decrements the variable value by 1 after executing the current statement.


Operators.pde
int var = 10;
println(var--);
println(var);

Output
10
9

Some more examples

Operators.pde

int var = 10;

int result = var++ + var++ ;
println("value of variable is " + var);
println("Result is " +result);

result = var++ + ++var;
println("value of variable is " + var);
println("Result is " +result);

result = var++ + var--;
println("value of variable is " + var);
println("Result is " +result);

result = --var + var--;
println("value of variable is " + var);
println("Result is " +result);

Output
value of variable is 12
Result is 21
value of variable is 14
Result is 26
value of variable is 14
Result is 29
value of variable is 12
Result is 26



Previous                                                    Next                                                    Home

No comments:

Post a Comment