//ForLoopsTest.java import java.awt.*; import javax.swing.*; public class ForLoopsTest extends JApplet { int count; public void init() { String input; input = JOptionPane.showInputDialog( "Enter number of times to loop" ); count = Integer.parseInt(input); /* for ("initializer" ; "condition" ; "update" ) { "body" } */ //loop just showing that it is looping: for (int i=1; i<=count; i++) //i is the "loop control variable" LCV JOptionPane.showMessageDialog(null, "i= " + i ); /* typically, in a counting loop the inititalizer declares and initializes the LCV to 1, update increments it, condition checks if it's not yet to the final value, body does something (maybe) with the value of the LCV. */ int sum=0, num, largest=Integer.MIN_VALUE; //input and sum a bunch of numbers. Any number of numbers. //Loop allows any/every amount of data to be processed. //Loop allows a bunch of code to executed any number of times. for (int i=1; i<=count; i++) { input = JOptionPane.showInputDialog( "Enter number " + i); num = Integer.parseInt(input); sum = sum + num; if (num > largest) largest = num; } JOptionPane.showMessageDialog(null, "Sum= " + sum + "\nMax= " + largest); //add these: //find min and max. //calculate average. //square root of each number. positives only. //counts of positive/negative odd/even //min, max length strings } public void paint (Graphics g) { for (int i=1; i<=count; i++) { //note it's the same loop g.setColor( Color.GREEN ); //down lefthand side: g.fillOval( 1, i*10, 10, 10 ); g.setColor( Color.RED ); //across top: g.fillOval( i*10, 1, 10, 10 ); g.setColor( Color.BLUE ); //diagonal: g.fillOval( i*10, i*10, 10, 10 ); } } }