Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Increment and Decrement Operations
-
Adding or subtracting 1 to a variable is probably one of the most computations
that we'll do. So there is a shortcut.
-
increment, ++, adds one to a variable.
-
decrement, --, subtracts one from the variable.
-
The outcome depends on where you put the variable.
-
postfix, or variable++, means that one will be added to the variable after
the statement is evaluated.
-
prefix, or ++variable, means that one will be added before the evaluation
takes place.
-
Let's take a look at each of these independently. What is the value
of total and item at the end of each line?
int total = 50;
int item = 5;
total = ++item;
total = item++;
total = --item;
total = item--;
Comparison Operators


Created by: Brandan
Jones January 4, 2002