Instructional Material: FOR Statement

The for statement is used to execute a block of statements some specific number of times. The general form of the for statement is:

The initialization-expression is executed once at entry into the for block, and sets up the initial value of a numeric variable (also called the index) used to control how many times the for statement is repeated. The termination-comparison is evaluated on each repeat of the for loop and the statement is repeated until the condition is false. Typically the index variable is compared to some numeric value. The increment-expression is executed after each repeat of the statement before the termination-comparison is checked and typically increments the index value. If after the initialization-expression is evaluated the termination-comparison is already false, the for statement is not executed. Remember the statement executed by the for can be a single statement or a block of statements in curly braces. Here is an example showing the use of for to iterate a number of times and use the iteration count:

In this example we initialize the index variable i to the starting value of 1, we will test i to be less than 4 (for continues while the comparison is true) and when we loop around we will increment i by 1 before checking the comparsion to see if the loop should continue. Here we start at 1, incement by 1 until i reaches 4. Since i will equal 4 at the start of the for block, the comparison would be false and we would stop. This means we would execute the statements 3 times. You can use any starting value, any comparision and any increment action that is logically correct.

For is typically used to repeat (or loop) the statement(s) a specific number of times or to access arrays or collections (discussed later) that use numeric indexes. Notice in the for statement above we (optionally) defined a new variable i to hold our numeric count. Variables defined in this way are accessible only within the for block. You can use a variable defined outside the for statement. Each component of the for statement is optional so you could write the above example like this:

In this case, since the variable i is defined outside the for statement, it will be accessible after the for is completed and in our example it would contain 3. Also note that the iteration and exit test are manually done in the loop, since they are not defined within the for statement. This for is useful if the exit test or iteration are complex or require multiple steps to determine.

The break and continue statements can be used in for loops. Break will end the loop and continue will jump back to the for skipping any remaining statements in the for block.

Watch this video about the for statement. Here is a detailed discussion of for. Here is the code for these examples on CodingGround. Add a for loop to the example to count from 3 down to 1.

 

Material Type: 
Lecture/Presentation
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
EV3
RoboRIO
Tetrix
SW Platform: 
Java
Interactivity Style: 
Mixed
Audience: 
Learner