The for loop gives us the same functionality as a while loop, with a more convenient way to keep track of loop counting variables. The general format is:
for (initialize; endingCondition; postLoopUpdate) {
action;
}
The initialize code is performed the first time only. Then endingCondition is checked. If it is true, the action(s) inside the loop are exectued. After the code in the loop is executed, the postLoopUpdate code is executed. The endingCondition is again checked, and the code in the loop is again executed, and so on, until the endingCondition is false.
Before studying the following examples, be sure to understand basic input and output using the Scanner class.
Consider the following examples. The first one shows how to display a multiplication list for a single number. For instance, selecting 3 would then display in a column the values 3, 6, 9, 12, 15, 18, ... and so on up to 36, which is 3 times 12. Using a while loop, this looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/**
* mult1.java - display mult. table from 1 - 12 for a #
*/
import java.util.Scanner; // for console IO
public class mult1
{
public static void main (String[] args)
{
int base,counter;
Scanner keyboard = new Scanner( System.in);
System.out.print("Please enter the base: ");
base = keyboard.nextInt();
//Now display the output
counter=1;
while (counter <= 12) {
System.out.println( counter * base);
counter++;
}
}//end main
} |
The equivalent using a for loop instead of a while loop is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/**
* mult2.java - display mult. table from 1 - 12 for a #
*/
import java.util.Scanner; // for console IO
public class mult2
{
public static void main (String[] args)
{
int base,counter;
Scanner keyboard = new Scanner( System.in);
System.out.print("Please enter the base: ");
base = keyboard.nextInt();
//Now display the output
for (counter=1; counter <=12; counter++) {
System.out.println( counter * base);
}
}//end main...
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/**
* mult3.java - display mult. table from start - end for a #
*/
import java.util.Scanner; // for console IO
public class mult3
{
public static void main (String[] args)
{
int base,counter;
int start, end;
Scanner keyboard = new Scanner( System.in);
System.out.print("Please enter the base, starting position, ending position: ");
base = keyboard.nextInt();
start = keyboard.nextInt();
end = keyboard.nextInt();
//Now display the output
for (counter=start; counter <=end; counter++) {
System.out.println( counter * base);
}
}//end main...
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/**
* mult4.java - display some rows of a mult. table from
* firstRow to lastRow, displaying columns
* up until the numColumns.
*/
import java.util.Scanner; // for console IO
public class mult4
{
public static void main (String[] args)
{
int base;
int firstRow, lastRow, numberOfColumns;
Scanner keyboard = new Scanner( System.in);
System.out.println("Please enter 3 integers for a multiplication table, ");
System.out.println("where the first two are the start and end rows, ");
System.out.println("and the third is the last column: ");
firstRow = keyboard.nextInt();
lastRow = keyboard.nextInt();
numberOfColumns = keyboard.nextInt();
// Each loop iteration displays a row
for( int i=firstRow; i<=lastRow; i++) {
// loop to display contents within each row
for( int j=1; j<=numberOfColumns; j++) {
System.out.printf ("%3d", i*j);
}
System.out.println();
}
}//end main...
}
|
[CS Dept] [UIC] [Prof. Reed]