Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007.

Similar presentations


Presentation on theme: "Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007."— Presentation transcript:

1 Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007

2 Lecture 7: Whiles - Indefinite Loops About Katie Coons Hometown: Falls Church, VA (near DC) First CS class: Summer 1998 DMP Summer Program, Summer 2004 BS in CS, University of Virginia, 2001 MS in CS, UT Austin, 2007? PhD in CS –Specialty in compilers and architecture –UT Austin, 2012? I hope? After that: Who knows?

3 Lecture 7: Whiles - Indefinite Loops Announcements & Review Announcements: Lab 2 Due –Thursday 10pm –1 file per pair Last Time: objects versus values strings & their operators

4 Lecture 7: Whiles - Indefinite Loops Review "excellence"a b String a = "excellence"; String b = a; alphabet abcdefghijklmnopqrstuvwyz String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); // ‘j’ int i1 = alphabet.indexOf(“de”); // 3

5 Lecture 7: Whiles - Indefinite Loops Review "excellence"a b String a = "excellence"; String b = “excellence”; alphabet abcdefghijklmnopqrstuvwyz String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); // ‘j’ int i1 = alphabet.indexOf(“de”); // 3 "excellence"

6 Lecture 7: Whiles - Indefinite Loops Today Input Using the Scanner class Indefinite Loops while (condition) { …. // somewhere in loop, condition becomes false …. }

7 Lecture 7: Whiles - Indefinite Loops Reading Input Features // Scanner is an “extra” Java class, so we // need to tell the compiler where to find it import java.util.*; // allocates an object of class Scanner Scanner stdin = new Scanner(System.in); String name = stdin.next(); int age = stdin.nextInt(); double commuteDistance = stdin.nextDouble(); boolean parent = stdin.nextBoolean(); // peeking to make sure the format is right // -- we will use these in Lecture 8 boolean isString = stdin.hasNext(); boolean isInt = stdin.hasNextInt(); boolean isDouble = stdin.hasNextDouble(); boolean isBoolean = stdin.hasNextBoolean();

8 Lecture 7: Whiles - Indefinite Loops Reading Input Example // Scanner is an “extra” Java class, so we // need to tell the compiler where to find it import java.util.*; // allocates an object of class Scanner Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.next(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + ".");

9 Lecture 7: Whiles - Indefinite Loops BlueJ Examples

10 Lecture 7: Whiles - Indefinite Loops Indefinite Loops boolean condition = true; // keep doing the loop while condition==true while (condition) { …. // somewhere in the loop the condition // becomes false. Why? if (test) { condition = false; } Lecture 7: While Loops

11 Lecture 7: Whiles - Indefinite Loops What does this do? int i = 0; while (i < 10) { System.out.println(“I’ve got soul, but I’m not a soldier.”); i++; }

12 Lecture 7: Whiles - Indefinite Loops What does this do? int i = 0; while (i < 10) { System.out.println(“I’ve got soul, but I’m not a soldier.”); i++; } // a “for” is more succinct in this case for (int i = 0; i < 10; i++) { System.out.println(“I’ve got soul, but I’m not a soldier.”); }

13 Lecture 7: Whiles - Indefinite Loops When do we while? boolean condition = true; // keep doing the loop while condition==true while (condition) { …. // somewhere in the loop the condition // becomes false. Why? if (test) { condition = false; }

14 Lecture 7: Whiles - Indefinite Loops Reading Input Features String word1 = stdin.next(); int number1 = stdin.nextInt(); double dnum1 = stdin.nextDouble(); boolean progress1 = stdin.nextBoolean(); // peeking to make sure the format is right boolean isString = stdin.hasNext(); boolean isInt = stdin.hasNextInt(); boolean isDouble = stdin.hasNextDouble(); boolean isBoolean = stdin.hasNextBoolean();

15 Lecture 7: Whiles - Indefinite Loops Reading Input Example Scanner stdin = new Scanner(System.in); // Is there anything wrong with this code? System.out.println("Enter a box width: "); int width = stdin.nextInt();

16 Lecture 7: Whiles - Indefinite Loops BlueJ Examples

17 Lecture 7: Whiles - Indefinite Loops Questions?


Download ppt "Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007."

Similar presentations


Ads by Google