Program 5.9 Use -= to count down by twos

As you might guess there is a corresponding = operator. If you wanted to count down from twenty to zero by twos you could write:

class CountToZeroByTwos  {

  public static void main (String args[]) {
    int i;
    for (i=20; i >= 0; i -= 2) {  
      System.out.println(i);
    } 
    System.out.println("All done!");

  }

} 
Here's the output.

% javac CountToZeroByTwos.java
% java CountToZeroByTwos
20
18
16
14
12
10
8
6
4
2
0
All done!
%

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home