RepetitionNew

RepetitionNew - Repetition Statements CS 180 Prof. Sunil...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Repetition Statements CS 180 Prof. Sunil Prabhakar Department of Computer Science Purdue University Sunil Prabhakar Problem n Write a game program that requires the user to guess a random integer. n After each input from the user Let the user know if the guess was correct Otherwise, inform the user that the guess was either too high or too low. n The game ends only when the user correctly guesses the value. 2 Sunil Prabhakar Repetition n To solve this problem, we need the ability to repeat a set of operations (get input, compare with secret and respond) an unknown number of times n The number is determined by how many guesses the user takes to get it right. n This week we will learn how to repeatedly execute portions of code using while , do- while , and for loops. 3 Sunil Prabhakar Guess 4 public class Guess { public static void main(String args){ int secret, guess; boolean done; Random random = new Random(); secret = random.nextInt(); done = false ; while (!done) { guess = Integer.parseInt(JOptionPane.showInputDialog( null , "Enter your guess." )); if (guess == secret){ done = true ; System.out.println( "You guessed correctly!" ); } else if (guess < secret) System.out.println( "Your guess was too low" ); else System.out.println( "Your guess was too high" ); } } } Sentinel while loop Sunil Prabhakar 5 Control Flow of while next statement; true false <get user guess> <print out message> <set done if appropriate> done = false; previous statement; !done ? Sunil Prabhakar while ( !done ) { g u e s s = I n t e g e r . p a r s e I n t ( ) ; if (guess == secret) ... } 6 Syntax for the while Statement while ( <boolean expression> ) <statement> while ( <boolean expression> ) { <statements> } boolean expression loop body is repeatedly executed as long as boolean expression is true Sunil Prabhakar 7 char grade; grade = JOptionPane.showInputDialog ( null , "Enter grade" ) .charAt(0); while ( grade < 'A' || grade > 'E' ) grade = JOptionPane.showInputDialog ( null , "Enter grade" ) .charAt(0); Example: input check n Only accepts grades 'A' through 'E' n Note: need for initial input before loop better option do-while loop Sunil Prabhakar 8 The do- while Statement char grade; do { grade = JOptionPane.showInputDialog ( null , "Enter grade" ) .charAt(0); } while ( grade < 'A' || grade > 'E' ) n Loop body executed before test (at least once) n No need for initial input before loop boolean expression Loop body executed once, and then repeatedly until boolean expression is false . Sunil Prabhakar Common Errors n Infinite loop if the loop condition never becomes false the loop body will be executed endlessly unless this is desired, ensure that the loop condition will change to false at some point...
View Full Document

Page1 / 33

RepetitionNew - Repetition Statements CS 180 Prof. Sunil...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online