Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Thinking in Java
Prev Contents / Index Next


A for loop performs initialization before the first iteration. Then it performs conditional testing and, at the end of each iteration, some form of “stepping.” The form of the for loop is:

for(initialization; Boolean-expression; step)

Any of the expressions initialization, Boolean-expression or step can be empty. The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes.

for loops are usually used for “counting” tasks:

// Demonstrates "for" loop by listing
// all the lowercase ASCII letters.
import com.bruceeckel.simpletest.*;

public class ListCharacters {
  static Test monitor = new Test();
  public static void main(String[] args) {
    for(int i = 0; i < 128; i++)
        System.out.println("value: " + i +
          " character: " + (char)i);
    monitor.expect(new String[] {
      "value: 97 character: a",
      "value: 98 character: b",
      "value: 99 character: c",
      "value: 100 character: d",
      "value: 101 character: e",
      "value: 102 character: f",
      "value: 103 character: g",
      "value: 104 character: h",
      "value: 105 character: i",
      "value: 106 character: j",
      "value: 107 character: k",
      "value: 108 character: l",
      "value: 109 character: m",
      "value: 110 character: n",
      "value: 111 character: o",
      "value: 112 character: p",
      "value: 113 character: q",
      "value: 114 character: r",
      "value: 115 character: s",
      "value: 116 character: t",
      "value: 117 character: u",
      "value: 118 character: v",
      "value: 119 character: w",
      "value: 120 character: x",
      "value: 121 character: y",
      "value: 122 character: z"
} ///:~

Note that the variable i is defined at the point where it is used, inside the control expression of the for loop, rather than at the beginning of the block denoted by the open curly brace. The scope of i is the expression controlled by the for.

This program also uses the java.lang.Character “wrapper” class, which not only wraps the primitive char type in an object, but also provides other utilities. Here, the static isLowerCase( ) method is used to detect whether the character in question is a lower-case letter.

Traditional procedural languages like C require that all variables be defined at the beginning of a block so that when the compiler creates a block, it can allocate space for those variables. In Java and C++, you can spread your variable declarations throughout the block, defining them at the point that you need them. This allows a more natural coding style and makes code easier to understand.

You can define multiple variables within a for statement, but they must be of the same type:

for(int i = 0, j = 1; i < 10 && j != 11; i++, j++)
  // body of for loop

The int definition in the for statement covers both i and j. The ability to define variables in the control expression is limited to the for loop. You cannot use this approach with any of the other selection or iteration statements.

The comma operator

Earlier in this chapter I stated that the comma operator (not the comma separator, which is used to separate definitions and method arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression, you can have a number of statements separated by commas, and those statements will be evaluated sequentially. The previous bit of code uses this ability. Here’s another example:

import com.bruceeckel.simpletest.*;

public class CommaOperator {
  static Test monitor = new Test();
  public static void main(String[] args) {
    for(int i = 1, j = i + 10; i < 5;
        i++, j = i * 2) {
      System.out.println("i= " + i + " j= " + j);
    monitor.expect(new String[] {
      "i= 1 j= 11",
      "i= 2 j= 4",
      "i= 3 j= 6",
      "i= 4 j= 8"
} ///:~

You can see that in both the initialization and step portions, the statements are evaluated in sequential order. Also, the initialization portion can have any number of definitions of one type.
Thinking in Java
Prev Contents / Index Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire