Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

The For Loop

by Emily Austin
Course: Java Programming I
Unit: Control Structures II
Topic: The for loop: single and nested
Class Duration: 50 minutes
Grade Level:
10th -12th
Objective
The students will be able to edit, save, compile and run a computer program written in the
programming language Java to code a solution using the for loop construct. The students will also be
able to do the same thing using nested for loops. They will achieve these objectives using BlueJ.
Materials
Lesson Plan
White board
Dry erase markers
Computers with BlueJ
Worksheet #1 (Java code that prints out five rows of asterisks, ten wide. The number of the row is the
first thing to be printed in the row, followed by the asterisks.)
Conversion Table Assignment
Background
This lesson assumes the level at which a student can read is at least equivalent to that of a typical 5th
grade student.
In order to be successful in this lesson, a student must have the following prior knowledge: use of
BlueJ, understanding of expressions and statements, Strings, Boolean operators, and ifelse
structure.
Procedures
1. Pre-assessment
A. Short review of expressions and statements
B. Short review of Boolean operators
C. Pose the question: How might you print out five rows of asterisks, ten wide?
2. Lecture
A. Basic forloop syntax
for, pair of parentheses with separating semicolons, body of loop surrounded by curly
brackets
initialization of loop control variable
test condition that controls loop entry
different ways to init/test i=0 vs. i=1 and i<5 vs. i<=5
expression that (hopefully) alters the loop control variable
B. Basic for loop logic

Initialization will always execute when a for loop is encountered. It only executes once.
If the test condition is true, then the body of the loop is entered.
If the test condition is false, then go to the line of code that immediately follows the
for loop body.
After the body of the for loop is executed, the loop control variable is updated.
Loop back to the test condition continuously until it is false.
C. Special cases (or pitfalls)
Extra semicolon in a statement -- for (int i=0; i<5; i++) ;
There may be more than one variable initialized in the for loop
for (int i=0, g=2; i<100; i++)
Test condition can test more than one thing for (int g=0; g<3 && h>1; ++g)
The loop control variable can be decremented also for (int g=5; g>=1; --g)
You can purposely leave out one or more portions of the for loop syntax out
(init; test; update), although the semicolons are still needed for placeholders
You may also leave out the body of the loop, although you need to add a semicolon after
the closing parenthesis for (x=0; x<1000000; ++x);
3. Guided Practice
A. Students will type the code, on the worksheet handed out, that prints out five rows of
asterisks, ten wide. The number of the row is the first thing to be printed in the row, followed by the
asterisks. They will save, compile this short program and run it using BlueJ.
B. Extra #1: Using a for loop, print out the first 10 even integers in a row.
C. Extra #2: Using a for loop, print out the first 10 odd integers in a row.
4. Lecture -- extra
A. Cover the basic logic of nested for loops
For each iteration of the outer loop, the inner loop is iterated from beginning to end.
5. Guided Practice -- extra
A. Have the students write a nested loop structure that consists of 5 rows and 3 columns. For
each iteration of the inner loop, print out the row and column pair. After the inner loop is iterated from
beginning to end, have a println statement execute to end the row.
6. Programming Assignment
A. Celsius to Fahrenheit conversion program.
B. extra -- Nested loop program if time allows. [Print out a right triangle of numbers. The
first row has a one, the second row has a one and a two, the third row has a one, two, and three, etc...
All of the rows are left justified.]
7. Evaluation
A. Student completes the programming assignment during the remainder of the class period.
B. Quiz at the end of the week covering syntax and for loop concepts.
8. Closing
A. Brief synopsis of what was covered.
B. Very brief preview of next days content.

Accommodations for Children with Special Needs


none

You might also like