Do/while Loops Another Type of Indefinite Loop. 2 The do/while loop do/while loop: Performs its test at the end of each repetition. –Guarantees that the.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

Building Java Programs
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Building Java Programs
CS305j Introduction to Computing While Loops 1 Topic 15 Indefinite Loops - While Loops "If you cannot grok [understand] the overall structure of a program.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops.
Building Java Programs Chapter 5
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
1 Sierpinski Valentine. CS 112 Introduction to Programming Switch; Text Processing Yang (Richard) Yang Computer Science Department.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic and Assertions reading: 5.3 – 5.5.
1 Text processing Readings: Characters char : A primitive type representing single characters. Individual characters inside a String are stored.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers; Type boolean reading: , 5.6.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CS 112 Introduction to Programming Boolean Type, String Processing Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 Building Java Programs Chapter 5 Lecture 12: Random, boolean Logic reading: 4.4, 5.1, 5.3, 5.6 (Slides adapted from Stuart Reges, Hélène Martin, and.
Topic 15 boolean methods and random numbers
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Categories of loops definite loop: Executes a known number of times.
Lecture 5: Program Logic
Lecture 8: The String class
Topic 16 boolean logic - George Boole
Building Java Programs
Building Java Programs

Building Java Programs
Building Java Programs Chapter 5
Type boolean boolean: A logical type whose values are true and false.
Building Java Programs Chapter 5
Lecture 4: Conditionals
Type boolean boolean: A logical type whose values are true and false.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 15 boolean methods and random numbers
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 16 boolean logic - George Boole
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Boolean logic suggested reading: 5.2.
Building Java Programs
Building Java Programs
Building Java Programs
Indefinite loop variations
Chapter 5 Lecture 5-3: Boolean Logic reading: 5.3, 5.4
Building Java Programs Chapter 4
Building Java Programs
Presentation transcript:

do/while Loops Another Type of Indefinite Loop

2 The do/while loop do/while loop: Performs its test at the end of each repetition. –Guarantees that the loop's {} body will run at least once. do { statement(s) ; } while ( test ); // Example: prompt until correct password is typed String phrase; do { System.out.print("Type your password: "); phrase = console.next(); } while (!phrase.equals("abracadabra"));

3 do/while question Modify the previous Dice program to use do/while = = = = = 7 You won after 5 tries! Is do/while a good fit for our past Sentinel program? do { // Roll // Sum tries++; } while (sum != 7);

Type boolean

5 Methods that are tests Some methods return logical values. –A call to such a method is used as a test in a loop or if. Scanner console = new Scanner(System.in); System.out.print("Type your first name: "); String name = console.next(); if (name.startsWith("Dr.")) { System.out.println("Will you marry me?"); } else if (name.endsWith("Esq.")) { System.out.println("And I am Ted 'Theodore' Logan!"); }

6 String test methods String name = console.next(); if (name.contains("Prof")) { System.out.println("When are your office hours?"); } else if (name.equalsIgnoreCase("ROGER")) { System.out.println("How about those Pats?"); } MethodDescription equals( str ) whether two strings contain the same characters equalsIgnoreCase( str ) whether two strings contain the same characters, ignoring upper vs. lower case startsWith( str ) whether one contains other's characters at start endsWith( str ) whether one contains other's characters at end contains( str ) whether the given string is found within this one

7 Type boolean boolean : A logical type whose values are true and false. –A logical test is actually a boolean expression. –It is legal to create a boolean variable pass a boolean value as a parameter return a boolean value from methods call a method that returns a boolean and use it as a test boolean minor = (age < 21); boolean isProf = name.contains("Prof"); boolean lovesCS = true; // allow who into club? if (minor || isProf || !lovesCS) { System.out.println("Can't enter the club!"); }

8 Using boolean Why is type boolean useful? –Can capture a complex logical test result and use it later –Can write a method that does a complex test and returns it –Makes code more readable –Can pass around the result of a logical test (as param/return) boolean goodAge = age >= 16 && age < 29; boolean goodHeight = height >= 78 && height < 84; boolean rich = salary >= ; if ((goodAge && goodHeight) || rich) { System.out.println("Okay, let's go out!"); } else { System.out.println("It's not you, it's me..."); }

9 Returning boolean public static boolean isPrime(int n) { int factors = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { factors++; } } if (factors == 2) { return true; } else { return false; } } Calls to methods returning boolean can be used as tests: if (isPrime(57)) {... }

10 Boolean question Write a "rhyme" / "alliterate" program to use boolean methods to test for rhyming and alliteration. Type two words: Bare blare They rhyme! They alliterate!

11 Boolean answer if (rhyme(word1, word2)) { System.out.println("They rhyme!"); } if (alliterate(word1, word2)) { System.out.println("They alliterate!"); }... // Returns true if s1 and s2 end with the same two letters. public static boolean rhyme(String s1, String s2) { // ? } // Returns true if s1 and s2 start with the same letter. public static boolean alliterate(String s1, String s2) { if (s1.startsWith(s2.substring(0, 1))) { return true; } else { return false; }