Java Coding 3 Over over again using while

  • Slides: 26
Download presentation
Java Coding 3 Over & over again: using while Sentinel-controlled input David Davenport Computer

Java Coding 3 Over & over again: using while Sentinel-controlled input David Davenport Computer Eng. Dept. , Bilkent University Ankara - Turkey. email: david@bilkent. edu. tr

IMPORTANT… n Students… This presentation is designed to be used in class as part

IMPORTANT… n Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not selfexplanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! n Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

Repetition n Java repetition statements while (condition) statement; do statement; while (condition); for (

Repetition n Java repetition statements while (condition) statement; do statement; while (condition); for ( init; condition; update) statement; n where n statement is any Java statement n condition is a boolean expression

the most general form of repetition WHILE…

the most general form of repetition WHILE…

The while statement n n does statementb “while” condition true does statementb zero or

The while statement n n does statementb “while” condition true does statementb zero or more times while (condition) statementb; loop true condition false statementb

Examples (1) n Print 5 stars ~asterisk characters~ n n n Use 5 println

Examples (1) n Print 5 stars ~asterisk characters~ n n n Use 5 println statements! Use a single println statement? Use automated repetition (a while loop!) stars. Printed is 0 while stars. Printed < 5 do print a star add 1 to stars. Printed print “done” * * * done

Examples (1) n Print 5 stars (alternative solution? ) n n n Use 5

Examples (1) n Print 5 stars (alternative solution? ) n n n Use 5 println statements! Use a single println statement? Use automated repetition (a while loop!) stars. Left. To. Print is 5 while there are stars. Left. To. Print do print a star subtract 1 from stars. Left. To. Print print “done” * * * done

Examples (1) n Printing 5 stars in Java stars. Printed = 0; while (stars.

Examples (1) n Printing 5 stars in Java stars. Printed = 0; while (stars. Printed < 5 ) { System. out. println( “*”); stars. Printed = stars. Printed + 1; } System. out. println( “done”); * * * done stars. Left. To. Print = 5; while (stars. Left. To. Print > 0) { System. out. println( “*”); stars. Left. To. Print = stars. Left. To. Print - 1; } System. out. println( “done”);

Examples (1) n Generalised forms ~how many solutions? * * * done count =

Examples (1) n Generalised forms ~how many solutions? * * * done count = 0; while ( count < 5 ) { System. out. println( “*”); count = 1; count = count + ) 1; { while ( count <= 5 } System. out. println( “*”); count = 1; System. out. println( “done”); count = count They all work correctly. while ( count < 6+ )1; { Which is preferred? } System. out. println( “*”); & why? count = -3; System. out. println( “done”); count = count while ( count <= + 5 1; ) { } System. out. println( “*”); System. out. println( “done”); count = count + 2; } System. out. println( “done”);

Examples (1) n Generalised forms ~how many solutions? count = 0; while ( count

Examples (1) n Generalised forms ~how many solutions? count = 0; while ( count < 5 ) { System. out. println( “*”); count = count + 1; } System. out. println( “done”); count = 5; while (count > 0) { System. out. println( “*”); count = count - 1; } System. out. println( “done”); * * * done If you print count next to “done”, what do you get? If you print count next to the star, what do you get?

Examples (2) n Read & sum 5 values n by analogy! sum is 0

Examples (2) n Read & sum 5 values n by analogy! sum is 0 count is 0 while _____ count < 5 do read value addadd value to to sumsum add 1 to count report sum 5 3 7 4 1 sum is 20

Examples (2) n Read & sum 5 values Named constant or ask for &

Examples (2) n Read & sum 5 values Named constant or ask for & get value from user sum = 0; count = 0; while ( count < 5 ) { value = scan. next. Int(); sum = sum + value; count = count + 1; } System. out. println( “sum is ” + sum); 5 3 7 4 1 sum is 20

Examples (3) n Extract design patterns/templates n Counting loop… count is 0 while count

Examples (3) n Extract design patterns/templates n Counting loop… count is 0 while count < number. Of. Repetitions process to repeat add 1 to count n Count, sum or product in loop… Immediately initialize to 0 (1) before loop

there are limits IN GENERAL…

there are limits IN GENERAL…

Generic form of while initialise any variables in condition while (test condition variable) do

Generic form of while initialise any variables in condition while (test condition variable) do statement & update condition variables; n What happens if you fail to… n Initialise loop variables • Unpredictable behaviour n Update loop variables • Infinite loop! (in console app’s use Ctrl-C to exit) n In general… n condition must be falsifiable by applying update to initial values… need proof!

Proving loops terminate (1) n Do these print “done”? count = 0; while (

Proving loops terminate (1) n Do these print “done”? count = 0; while ( count < 5 ) { System. out. println( “*”); count = count - 1; } System. out. println( “done”); i = 1; while ( i != 50 ) { System. out. println( i); i = i + 2; } System. out. println( “done”); How can you decide? • Try it? • Logic?

Proving loops terminate (2) n What about this one? i = scan. next. Int();

Proving loops terminate (2) n What about this one? i = scan. next. Int(); while ( i != 1 ) { if ( i % 2 == 0) i = i / 2; else i = 3 * i + 1; } System. out. println( “done”); Collatz conjecture n Would really like a program that took our algorithm as input and reported whether it would “halt” or not. n Surprisingly, we do have a proof that no such program can be written. The Halting Problem – Alan Turing – 1937 “On computable numbers, with an application to the Entscheidungsproblem”

alternative forms of DATA INPUT…

alternative forms of DATA INPUT…

Reading a set of data n Three approaches How many? 5 3 5 7

Reading a set of data n Three approaches How many? 5 3 5 7 4 1 sum is 20 More? 3 More? 5 More? 7 More? 4 More? 1 More? sum is Y Y 3 Must say 5 “no more” 7 4 1 -1 sum is 20 Y N 20 Sentinel value non-data value marks end of list

Sentinel-controlled input…? n Read & sum values until sentinel sum is 0 value read

Sentinel-controlled input…? n Read & sum values until sentinel sum is 0 value read is -27 _____ while value != -1 dodo read value addadd value to to sumsum value read value report sum 5 3 7 4 1 -1 sum is 20

Sentinel-controlled input n Sum set of values terminated by -1 sum = 0 read

Sentinel-controlled input n Sum set of values terminated by -1 sum = 0 read value while value is not -1 do add value to sum read next value print sum n Extract another design pattern read value while value is not the sentinel process the value read next value Must • process every value (inc. first) • NOT process sentinel • work for empty set

Examples (sentinel input) n Find the average of set of exam scores. Allow user

Examples (sentinel input) n Find the average of set of exam scores. Allow user to enter any number of scores; stop and report the average when a negative score is entered. n Write a program that allows the user to enter a set of values. When the user enters zero it should stop and report the number of positive and the number of negative values entered. n 5, 3, -7, 2, -4, 0 3 positive & 2 negative values entered. n 0 0 positive & 0 negative values entered. n 9, 2, 8, 0 3 positive & 0 negative values entered. n -4, -8, -3, -5, 0 0 positive & 4 negative values entered.

Examples (sentinel input) n Allow user to enter a limit value, followed by a

Examples (sentinel input) n Allow user to enter a limit value, followed by a set of positive values terminated by a zero. After the user enters zero, print a message to say whether any of the values exceeded the given limit or not. Enter limit: 8 Enter values (0 to stop): 5 2 7 4 0 Limit NOT exceeded. Enter limit: 10 Enter values (0 to stop): 3 12 7 14 6 0 Limit WAS exceeded.

questions FOR YOU…

questions FOR YOU…

Questions (1) n Read a set of positive values & report their maximum after

Questions (1) n Read a set of positive values & report their maximum after the user enters a negative value. Extend to minimum. n Allow the user to enter a set of positive values & report whether they were in ascending order or not. n Write a program that asks the user to enter an integer number N, then prints a triangle with N lines of stars or numbers. For example, if N is 4 * ******* 1 222 33333 4444444

Questions (2) n Compute PI using the series expansion PI = 4 * (

Questions (2) n Compute PI using the series expansion PI = 4 * ( 1 - 1/3 + 1/5 - 1/7 + 1/9 …) by evaluating: (a) a fixed number of terms (b) enough terms until a specified accuracy is achieved, e. g. until the value changes < 0. 001 n For intervals of 0. 1 seconds, compute and print the height of a mass falling from a given initial height, until it hits the ground.