1 / 28

CSCI S-1 Section 7

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?

niel
Download Presentation

CSCI S-1 Section 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI S-1Section 7

  2. 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

  3. Loops When do you want to use a For Loop? What are the alternatives?

  4. 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.

  5. While Loops, Example 1 inti = 0; while (i < 10) { i += 2; System.out.println(i); } Answer: 2 4 6 8 10

  6. While Loops, Example 2 inti = 81; while (i % 3 == 0) { i /= 3; System.out.println(i); } Answer: 27 9 3 1

  7. While Loops, Example 3 inti = 81; while (i % 3 == 0) { System.out.println(i); i /= 3; } Answer: 81 27 9 3

  8. 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(); }

  9. While Loops, Example 5 //reconstruct this as a while loop For (inti = 2; i < 10; i++) { System.out.println("Hello from " + i + "!") }

  10. 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++; }

  11. 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) { } }

  12. 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(); } } }

  13. 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

  14. Do-While // prompt user (repeatedly) to enter 1 or 2

  15. 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);

  16. 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?

  17. 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?

  18. 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

  19. 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 { }

  20. 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); } } }

  21. 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?

  22. 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')

  23. 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?

  24. 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?

  25. String Exercises //use one Java statement to print string s, one char at a time

  26. 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’

  27. 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));

  28. 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

More Related