Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSCI S-1 Section 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 int i = 0; while (i < 10) { i += 2; System.out.println(i); } Answer: 2 4 6 8 10

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

7 While Loops, Example 3 int i = 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); int i = 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 (int i = 2; i < 10; i++) { System.out.println("Hello from " + i + "!") }

10 While Loops, Example 5 //reconstruct this as a while loop For (int i = 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: "); int currentNumber = kbd.nextInt(); int previousNumber = 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); int i; 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'; int i = (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'; int i = (int) ch; System.out.println(i);// prints 65 //typecast an int into a char int i = 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'; int i = (int) ch; System.out.println(i);// prints 65 //typecast an int into a char int i = 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 (int i = 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 int len= 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 int len= 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 (int i = 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 (int i = 0; i < s.length(); i++) System.out.println(s.charAt(i)); //now print it backwards for (int i = s.length() - 1; i >= 0; i--) System.out.print(s.charAt(i)); //now print only the lowercase letters between ‘g’ and ‘q’ For (int i = 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


Download ppt "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."

Similar presentations


Ads by Google