280 likes | 380 Views
CSCI S-1 Section 7. Coming Soon. Problem Set Three, Part B Tuesday, July 14, 17:00 EST Problem Set Four ( 72 + 5/15 points) Friday, July 17, 17:00 EST. Loops. When do you want to use a For Loop? What are the alternatives?. While Loops. When do you want to use a For Loop?
E N D
Coming Soon • Problem Set Three, Part B • Tuesday, July 14, 17:00 EST • Problem Set Four (72 + 5/15 points) • Friday, July 17, 17:00 EST
Loops When do you want to use a For Loop? What are the alternatives?
While Loops When do you want to use a For Loop? What are the alternatives? while (condition) { // loop code goes here; } where condition is some expression that evaluates to a boolean. At the start of every iteration of a while-loop, Java will evaluate the expression. If it's true, the loop will execute; if not, the loop will stop, and program execution will go on to the next line after the loop.
While Loops, Example 1 inti = 0; while (i < 10) { i += 2; System.out.println(i); } Answer: 2 4 6 8 10
While Loops, Example 2 inti = 81; while (i % 3 == 0) { i /= 3; System.out.println(i); } Answer: 27 9 3 1
While Loops, Example 3 inti = 81; while (i % 3 == 0) { System.out.println(i); i /= 3; } Answer: 81 27 9 3
While Loops, Example 4 //when does this loop terminate? Scanner console = new Scanner(System.in); inti = console.nextInt(); while (i > 0 || i < 10) { System.out.println(i); i= console.nextInt(); }
While Loops, Example 5 //reconstruct this as a while loop For (inti = 2; i < 10; i++) { System.out.println("Hello from " + i + "!") }
While Loops, Example 5 //reconstruct this as a while loop For (inti = 2; i < 10; i++) { System.out.println("Hello from " + i + "!") } int i = 2; while (i < 10) { System.out.println("Hello from " + i + "!"); i++; }
While Loops, Example 6 //Read int temperatures from the keyboard until there’s an increase //Inside the loop we read #s and compare with the prior # //Loop stops when the current # is greater than the prior # Class NoIncreases { public static void main (String[] args) { } }
While Loops, Example 6 import java.util.*; Class NoIncreases { public static void main (String[] args) { Scanner kbd = new Scanner(System.in); System.out.println("Enter a number: "); intcurrentNumber = kbd.nextInt(); intpreviousNumber = currentNumber; // so loop will start while (currentNumber <= previousNumber) { previousNumber = currentNumber; System.out.println("Enter a number: "); currentNumber = kbd.nextInt(); } } }
Do-While // do loops always execute at least once; while loops may never execute // do loops can be helpful for obtaining/initializing variables do { // loop code goes here; } while (condition) ; // note the semi-colon
Do-While // prompt user (repeatedly) to enter 1 or 2
Do-While // prompt user (repeatedly) to enter 1 or 2 import java.util.*; … Scanner keyboard = new Scanner(System.in); inti; do { System.out.print("Please enter 1 or 2: "); i = keyboard.nextInt(); } while (i != 1 && i != 2);
Characters and ASCII // roman letters and numbers are encoded sequentially // 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122 //typecast a char into an int char ch = 'A'; inti = (int) ch; System.out.println(i); // what does this print?
Characters and ASCII // roman letters and numbers are encoded sequentially // 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122 //typecast a char into an int char ch = 'A'; inti = (int) ch; System.out.println(i); // prints 65 //typecast an int into a char inti = 90; char ch = (char) i; System.out.println(ch); // what does this print?
Characters and ASCII // roman letters and numbers are encoded sequentially // 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122 //typecast a char into an int char ch = 'A'; inti = (int) ch; System.out.println(i); // prints 65 //typecast an int into a char inti = 90; char ch = (char) i; System.out.println(ch); // prints Z
Characters and ASCII //print out an encoding table of all capital letters // 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122 class Ascii { }
Characters and ASCII //print out an encoding table of all capital letters // 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122 class Ascii { public static void main(String[] args) { for (inti = 65; i <= 90; i++) { System.out.println(i + "\t" + (char) i); } } }
Characters and ASCII //Given some char ch. How could we determine if ch is a capital vowel? //How could we determine if ch is between the letters `b' and `y', inclusive?
Characters and ASCII //Given some char ch. How could we determine if ch is a capital vowel? ch == `A' || ch == `E' || ch == `I' || ch == `O' || ch == `U’ //How could we determine if ch is between the letters `b' and `y', inclusive? (ch >= `b' && ch <= `y')
Character Strings String s= new String(); //make an empty string String s= “Hello, World!”; //assign a literal to a string intlen= s.length(); //what is len?
Character Strings String s= new String(); //make an empty string String s= “Hello, World!”; //assign a literal to a string intlen= s.length(); //len is 13 //indexing is zero-based char charAt(pos); //returns char at pos in some string s.charAt(4) == s.charAt(8); //true or false? s.charAt(0) <= s.charAt(7); //true or false?
String Exercises //use one Java statement to print string s, one char at a time
String Exercises //use one Java statement to print string s, one char at a time for (inti = 0; i < s.length(); i++) System.out.print(s.charAt(i)); //now print it backwards //now print only the lowercase letters between ‘g’ and ‘q’
String Exercises //use one Java statement to print string s, one char at a time for (inti = 0; i < s.length(); i++) System.out.println(s.charAt(i)); //now print it backwards for (inti= s.length() - 1; i >= 0; i--) System.out.print(s.charAt(i)); //now print only the lowercase letters between ‘g’ and ‘q’ For (inti = 0; i < s.length(); i++) if (s.charAt(i) >= ‘g’ && s.charAt(i) <= ‘q’) System.out.print(s.charAt(i));
More String Methods substring() s.substring(7) “World!” //substring from pos 7 to end s.substring(0,5) “Hello” //substring betw pos 0 and 4, inc s.substring(7).charAt(0) ‘W’ //why? toUpperCase(), toLowerCase() //return UC/LC string s.substring(0,5).toUpperCase() //what’s the difference? s.toUpperCase().substring(0,5) indexOf() //charAt in reverse -- s.indexOf(‘W’) 7 //first instance of char ‘W’ in s s.indexOf(“World”) 7 //first instance of String “World” in s equals() // == doesn’t work with Strings s.equals(s2) //compares Strings s and s2