O'Reilly logo

Java™ How to Program, Seventh Edition by P. J. Deitel - Deitel & Associates, Inc., H. M. Deitel - Deitel & Associates, Inc.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.4. Examples Using the for Statement

The following examples show techniques for varying the control variable in a for statement. In each case, we write the appropriate for header. Note the change in the relational operator for loops that decrement the control variable.

  1. Vary the control variable from 1 to 100 in increments of 1.

    for ( int i = 1; i <= 100; i++ )
  2. Vary the control variable from 100 to 1 in decrements of 1.

    for ( int i = 100; i >= 1; i-- )
  3. Vary the control variable from 7 to 77 in increments of 7.

    for ( int i = 7; i <= 77; i += 7 )
  4. Vary the control variable from 20 to 2 in decrements of 2.

    for ( int i = 20; i >= 2; i -= 2 )
  5. Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17, 20.

    for ( int i = 2; i <= 20 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required