LESSON THREE LOOPS. Open in Notepad++ for line numbers // Steve Eilertsen // Program does not run. All input from JOptionPane is String. // Line 29 gives an error because we are trying to store a String into // an integer variable. import javax.swing.JOptionPane; public class Loops { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); int times = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); for(int x = 0; x < times; x++) { System.out.println("hello " + x); } } // end main } // end class ============================================== // This program runs because the JOptionPane String variable in line 67 // is parsed and stored in an integer in line 69 import javax.swing.JOptionPane; public class Loops2 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); // The for method has three arguements (parameters separated by semi colons) // 1) Declare and initialise x. // 2) The condition follows. If true, the loop will execute. // 3) The value of x is increasd by one. for(int x = 0; x < times; x++) { System.out.println("hello " + x); } } // end main } // end class =============================================== // Adds a while loop. This program does not run because it wants to use // the variable x which has not been declared. import javax.swing.JOptionPane; public class Loops3 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); // x below is local to the for loop ONLY. for(int x = 0; x < times; x++) { System.out.println("hello for loop: " + x); } // x no longer exists. while(x < times) { } } // end main } // end class =========================================== // This program runs because x has been declared in line 174. // The variable x is declared an integer and initialised to zero. // At this point the while loop does not do anything but it does run. // Unfortunaley it is an infinate loop because the condition is never met. // The value x is always smaller than the variable "times" and therefore the // loop continues to run indefinatley. import javax.swing.JOptionPane; public class Loops4 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); for(int x = 0; x < times; x++) { System.out.println("hello for loop: " + x); } // We need to declare x so that the while loop can use it int x = 0; // This is an infinate loop. while(x < times) { } } // end main } // end class ============================================= // Here the while loop is not infinite. The value of x is increased in line 231, // so the condition is met and the loop terminates. import javax.swing.JOptionPane; public class Loops5 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); for(int x = 0; x < times; x++) { System.out.println("hello for loop: " + x); } int x = 0; // This loop does terminate because x increases in size // until the condition is met while(x < times) { x++; } } // end main } // end class =============================================== // The while loop finally does something import javax.swing.JOptionPane; public class Loops6 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); for(int x = 0; x < times; x++) { System.out.println("hello for loop: " + x); } int x = 0; while(x < times) { System.out.println("hello while loop: " + x); x++; } } // end main } // end class ============================================= // Another for loop is coded using char but it does not run. // This is because the variable x used for the while still exists - it is local // to the whole main method. Therefore a second x cannot be declared by the // new for loop. import javax.swing.JOptionPane; public class Loops7 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); for(int x = 0; x < times; x++) { System.out.println("hello for loop: " + x); } int x = 0; while(x < times) { System.out.println("hello while loop: " + x); x++; } // the variable x still exists and cannot be declared a second time. for(char x = 'A'; x < 'Z'; x++) { System.out.println("char"); } } // end main } // end class ============================================ // This runs. The for loop loops from "A" to "Z". The variable y increases // each time by one, moving from letter to letter in the alphabet import javax.swing.JOptionPane; public class Loops8 { public static void main(String[]args ) { JOptionPane.showMessageDialog(null, "hello world" ); String myName = JOptionPane.showInputDialog(null, "Input your name?" ); myName = myName.toUpperCase( ); if (myName.equals("STEVE")) { System.out.println("Bingo"); } System.out.println("Good morning " + myName); String timesSt = JOptionPane.showInputDialog(null, "How many times must the loop run?" ); int times= Integer.parseInt(timesSt); for(int x = 0; x < times; x++) { System.out.println("hello for loop: " + x); } int x = 0; while(x < times) { System.out.println("hello while loop: " + x); x++; } // Loop starts at A and terminates with Z for(char y = 'A'; y <= 'Z'; y++) { System.out.println("Char: " + y); } } // end main } // end class ======================================== FINAL OUTPUT ============ ----jGRASP exec: java Loops8 Bingo Good morning STEVE hello for loop: 0 hello for loop: 1 hello for loop: 2 hello for loop: 3 hello for loop: 4 hello while loop: 0 hello while loop: 1 hello while loop: 2 hello while loop: 3 hello while loop: 4 Char: A Char: B Char: C Char: D Char: E Char: F Char: G Char: H Char: I Char: J Char: K Char: L Char: M Char: N Char: O Char: P Char: Q Char: R Char: S Char: T Char: U Char: V Char: W Char: X Char: Y Char: Z ----jGRASP: operation complete.