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

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

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

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

Lecture 7: Whiles - Indefinite Loops

Input and while loops

Katie CoonsCS 305J

February 2, 2007

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

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?

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

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

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

Lecture 7: Whiles - Indefinite Loops

Review

"excellence"a

b

String a = "excellence";String b = a;

alphabet

a b c d e f g h i j k l m n o p q r s t u v w y z

String alphabet = "abcdefghijklmnopqrstuvwxyz";

char c1 = alphabet.charAt(9); // ‘j’int i1 = alphabet.indexOf(“de”); // 3

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

Lecture 7: Whiles - Indefinite Loops

Review

"excellence"a

b

String a = "excellence";String b = “excellence”;

alphabet

a b c d e f g h i j k l m n o p q r s t u v w y z

String alphabet = "abcdefghijklmnopqrstuvwxyz";

char c1 = alphabet.charAt(9); // ‘j’int i1 = alphabet.indexOf(“de”); // 3

"excellence"

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

Lecture 7: Whiles - Indefinite Loops

Today

InputUsing the Scanner class

Indefinite Loops while (condition) {

…. // somewhere in loop, condition becomes false

…. }

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

Lecture 7: Whiles - Indefinite Loops

Reading Input Features

// Scanner is an “extra” Java class, so we// need to tell the compiler where to find itimport java.util.*;// allocates an object of class ScannerScanner 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 8boolean isString = stdin.hasNext();boolean isInt = stdin.hasNextInt();boolean isDouble = stdin.hasNextDouble();boolean isBoolean = stdin.hasNextBoolean();

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

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 ScannerScanner 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 + ".");

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

Lecture 7: Whiles - Indefinite Loops

BlueJ Examples

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

Lecture 7: Whiles - Indefinite Loops

Indefinite Loops

boolean condition = true;// keep doing the loop while condition==truewhile (condition) {

….// somewhere in the loop the condition // becomes false. Why?if (test) {

condition = false;}

}

Lecture 7: While Loops

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

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++;

}

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

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 casefor (int i = 0; i < 10; i++) {System.out.println(“I’ve got soul, but I’m not a soldier.”);

}

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

Lecture 7: Whiles - Indefinite Loops

When do we while?

boolean condition = true;// keep doing the loop while condition==truewhile (condition) {

….// somewhere in the loop the condition

// becomes false. Why?if (test) {

condition = false; }}

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

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();

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

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();

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

Lecture 7: Whiles - Indefinite Loops

BlueJ Examples

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

Lecture 7: Whiles - Indefinite Loops

Questions?