write a program that
play

Write a program that } Reads an integer value from the user and - PowerPoint PPT Presentation

Write a program that } Reads an integer value from the user and prints a message, including the value, to the screen. } Thats easy weve done very similar things already. } Reads 10 integer values from the user and prints their mean to


  1. Write a program that… } Reads an integer value from the user and prints a message, including the value, to the screen. } That’s easy – we’ve done very similar things already. } Reads 10 integer values from the user and prints their mean to the screen. } Hmmm, that shouldn’t be too hard. } But it’s repetitive. Class #13: } And boring. Repetition with Loops } Reads 1000 integer values from the user and prints their mean to the screen. Software Design I (CS 120): D. Mathias } Uhhh, no. 2 Software Design I (CS 120) Repetition with Loops General Types of Loops } Often, we want to repeat some operation in a program } Loops can be broken down into two types: more than once Definite: when we write these loops, we know exactly 1. how many times we want to run them } Loops allow us to do this without needing to repeat the same code over and over ourselves Add up the integers from 1 to 1,000,000 } Precise starting and stopping conditions Print out the first 100 prime numbers } Control exactly how many times we do something if needed Indefinite: when we write these loops we do not 2. } When combined with conditional execution, looping always know exactly how many times they will run allows us to create far more complex and interesting Read integers from user until they enter -1 programs, behaving in a variety of sophisticated ways Divide input by 3 until it is less than 2 3 4 Software Design I (CS 120) Software Design I (CS 120) 1

  2. Looping with while Comparing if and while } The if conditional has a simple structure: } The while loop can be used for either definite or indefinite cases (most common kind for indefinite) (a) boolean expression is (b) if false , skip the if ( condition ) evaluated instructions and move on to { } Simple syntax: rest of program instructions; } boolean expression (c) if true , do the instructions, then move on // rest of program while ( condition ) (exactly like an to rest of program { if -condition) instructions; } A while loop has similar structure, but more complex behavior: } (a) boolean expression is (b) if false , skip the while ( condition ) evaluated instructions and move on to Code to repeat (as long { rest of program instructions; as condition holds) (c) if true , do the } instructions, then repeat // rest of program the process all over again, starting again at step (a) 5 6 Software Design I (CS 120) Software Design I (CS 120) A Definite Example A Definite Example } Every loop has four parts: int sum = 0; } Initialization : establish int count = 1; the state before looping int sum = 0; while (count < 6 ) int count = 1; { } Condition : boolean to sum = sum + count; System.out.println( count + ". " + sum ); while ( count < 6 ) control when loop stops count++; { } } Main work : code to be sum = sum + count; repeatedly executed count++; Runs exactly 5 times, every time. } Make progress : code to } (Why?) move the loop toward termination by eventually changing the condition 7 8 Software Design I (CS 120) Software Design I (CS 120) 2

  3. An Indefinite Example An Indefinite Example System.out.print("Enter a value: "); System.out.print("Enter a value: "); Scanner scan = new Scanner( System.in ); Scanner scan = new Scanner( System.in ); } Every loop has four parts: int i = scan.nextInt(); int i = scan.nextInt(); } Initialization : establish int log = 0; int log = 0; the state before looping while ( i > 1 ) while ( i > 1 ) } Condition : boolean to { { log++; control when loop stops log++; i = i / 2; } Main work : code to be i = i / 2; } repeatedly executed } } Update : code to move System.out.println( "log_2 = " + log ); System.out.println( "log_2 = " + log ); the loop toward termination by eventually Will run differently depending upon input given. changing the condition If input i > 1 , runs log 2 (i) times. If input i <= 1 , then never runs at all. 9 10 Software Design I (CS 120) Software Design I (CS 120) Tracing Loop Operations Controlling Repetition } Original loop: int sum = 0; int sum = 0; int count = 1; int count = 1; sum == 1+2+3+4+5 while ( count < 6 ) while ( count < 6 ) { { sum = sum + count; sum = sum + count; count++; System.out.println( … ); } count++; } Change it to generate: } Iteration # count < 6? sum count } sum == 1+2+3+…+25 initialize N/A 0 1 while ( count < 26 ) 1 true 1 2 int count = 3; 2 true 3 3 } sum == 3+4+5+…+25 ... 3 true 6 4 while ( count < 26 ) 4 true 10 5 while ( count < 12 ) 5 true 15 6 } sum == 1+3+5+…+11 ... 6 false N/A N/A count = count + 2 11 12 Software Design I (CS 120) Software Design I (CS 120) 3

  4. How Many Times will a Loop Run? Using Loops } Sam earns $100 per day with int sum = 0; int sum = 0; a daily raise of $100 . int count = 1; int count = 99; while ( count <= 100 ) { } Sue earns $0.01 per day with while ( count > 0 ) { initialize prior- double sam = 100.0; sum = sum + count; a salary that doubles every day. sum = sum + count; double sue = 0.01; state variables count = count + 2; count = count - 2; } How many days pass before // day counter } } int count = 0; Sue’s total income actually exceeds Sam’s? Runs 50 times: // loop until sue earns more Runs 50 times: while ( sue < sam ) 99, 97, 95, …, 1 1, 3, 5, …, 99 { Note: this code boolean // count one day at time int sum = 0; int sum = 0; sample actually condition count++; int count = 0; int count = 99; calculates the day // increase salaries while ( count > 0 ) { while ( count != 0 ) { on which Sue’s sam = sam + 100.0; sum = sum + count; sum = sum + count; daily pay exceeds sue = sue * 2; work to do count = count - 2; count = count - 2; } Sam’s. We need to } } keep track of // print out final results progress the System.out.println( … ); running total as well Runs Many, Many Times! Never runs at all. loop variables (see demo code) 13 14 Software Design I (CS 120) Software Design I (CS 120) 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend