For Loop
The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:
for (initialization; termination; increment)
{
statement(s)
}
When using this version of the for statement, keep in mind that:
- The initialization expression initializes the loop; it's executed once, as the loop begins.
- When the termination expression evaluates to false, the loop terminates.
- The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
The following program, ForDemo, uses the general form of the for statement to print the numbers 1 through 10 to standard output:
class ForDemo
{
public static void main(String[] args)
{
for(int i=1; i<11; i++)
{
System.out.println("Count is: " + i);
}
}
}
The output of this program is:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
Notice how the code declares a variable within the initialization expression. The scope of this variable extends from its declaration to the end of the block governed by the for statement, so it can be used in the termination and increment expressions as well. If the variable that controls a for statement is not needed outside of the loop, it's best to declare the variable in the initialization expression. The names i, j, and k are often used to control for loops; declaring them within the initialization expression limits their life span and reduces errors.
The three expressions of the for loop are optional; an infinite loop can be created as follows:
for ( ; ; )
{ // infinite loop
// your code goes here
}
For each of the Programming 1 lessons you are strongly encouraged to view and play around with the sample code provided in the BlueJ Java Student Samples folder.
Open BlueJ and choose Open Project from the Project menu. Navigate to your Programming 1 folder on the I: drive and open the BlueJ Java Student Samples folder. Open the project named Loops.
Lesson Example: Project Loops, Class For_Loop, methods loopIt(), loopWithBlock(), loopByUserInput(), loopItBackwards() and usingBreak()
- Right-click on the Class named For_Loop and select new For_Loop()
- Right-click on the red rectangle and choose the method named loopIt()
- Right-click on the red rectangle and choose the method named loopWithBlock()
- Right-click on the red rectangle and choose the method named loopByUserInput()
- Right-click on the red rectangle and choose the method named loopItBackwards()
- Right-click on the red rectangle and choose the method named usingBreak()
Code for method loopIt():
void loopIt()
{
for(int i=1; i<=10; i++)// hey, where is the variable 'i' declared?
System.out.println("This is looping 10 times!");
}
Code for method loopWithBlock():
void loopWithBlock()
{
for(int loopy = 1; loopy<=5; loopy++)
{
System.out.println("This is trip number " + loopy + " through the loop.");
System.out.println("Loops are sooo cool.");
}
}
Code for method loopByUserInput():
void loopUpByUserInput()
{
int goUpBy;
System.out.print("Enter a number: ");
goUpBy = in.nextInt();
for(int i=1; i<=100; i+=goUpBy)
System.out.println("The value of i is " + i);
}
Code for method loopItBackwards():
void loopItBackwards()
{
for(int i=10; i>=1; i--)
System.out.println("The value of i is " + i);
}
Code for method usingBreak():
void usingBreak()
{
int stopValue;
System.out.print("Enter a number from 1 to 10: ");
stopValue = in.nextInt();
for(int i=10; i>=1; i--)
{
System.out.println("The value of i is " + i);
if(i==stopValue)
{
System.out.println("I have to leave the loop now.");
break;
}
}
}