190 likes | 318 Vues
Java Coding 3. Over & over again!. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…
E N D
Java Coding 3 Over & over again! David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr
IMPORTANT… • Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! 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! • 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 • Java repetition statements while (condition) statement; do statement; while (condition); for ( init; condition; update) statement; • where • statement is any Java statement • condition is a boolean expression
condition true statement false The while statement • Does statement while condition true • does statement 0 or more times while (condition) statement;
Examples (1) • Print 5 asterisk characters • Use 5 println statements! • Use a single println statement! • Use a while loop… *****done count = 0;while ( count < 5 ) { System.out.println( “*”); count = count + 1;}System.out.println( “done”); If you print out count as well as the asterisk, what do you get?
Examples (2) • Read & sum 5 values • By analogy… 53741sum is 20 sum = 0;count = 0;while ( count < 5 ) { value = scan.nextInt(); sum = sum + value; count = count + 1;}System.out.println( “sum is ” + sum);
Examples (3) • Extract design patterns/templates • Counting loop… • Count, sum or product in loop… count = 0;while ( count < numberOfRepetitions ) {// process to repeat count = count + 1;} Immediately initialize to 0 (1) before loop
Generic form of while • What happens if you fail to… • Initialise loop variables • Unpredictable behaviour • Update loop variables • Infinite loop! (in console app’s use Ctrl-C to exit) • In general… • condition must be falsifiable by applying update to initial values… need proof! Initialise any variables in conditionwhile (test condition variable) do statement & update condition variables;
Proving loops terminate (1) • 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”);
Proving loops terminate (2) • What about this one? i = scan.nextInt(); while ( i != 1 ) { if ( i % 2 == 0) i = i / 2; else i = 3 * i + 1;}System.out.println( “done”); • Proof that cannot always write program to determine whether given algorithm will halt or not. (ref. Halting Problem – Alan Turing.)
Sentinel valuenon-data valuemarks end of list Reading set of data • Three approaches How many? 5 35741sum is 20 More? Y 3 More? Y 5More? Y 7More? Y 4More? Y 1More? N sum is 20 35741-1 sum is 20
Sentinel-controlled input • Sum set of values terminated by -1 sum = 0 read value while value is not the sentinel do add value to sum read next value print sum • Extract another design pattern read value while value is not the sentinel process the value read next value
Examples (sentinel input) • Find average of set of exam scores. • Count number of positive & negative values entered by user. Use zero to stop input. • Allow user to enter a set of positive values terminated by a zero. When the user enters zero, print a msg to indicate whether any value exceeded a threshold or not. • Read a set of positive values & report whether they were in ascending order or not. • Read a set of positive values & report their maximum. Extend to minimum.
init condition false true statement update Java for statements • Same as while loop! • Use as short-hand counting style loop for ( init; condition; update) statement; Example: for ( i = 0; i < 5; i = i + 1) System.out.println( “*”);
statement condition true false Java do-while statements • Repeat 1 or more times do statement; while (condition); Example: i = 0;do { System.out.println( “*”); i++; } while ( i < 5);
Examples (do-while) • Data validation • e.g. Read positive value from user do ask for “a positive value” and get valuewhile value is not positive do { System.out.print( “Enter positive value: ”); value = scan.nextInt();} while ( value <= 0);// assert: value > 0
Examples (do-while) • Menus - set of options for user to select from do display menu get selection from user perform selection while selection is not exit print “goodbye” ABC Trading Co.------------------ 1 – sales 2 – stock 3 – admin Select (0 to exit): _ if selection is SALES then // do sales things else if selection is STOCK then // do stock things else if selection is ADMIN then // do admin things else if selection is not EXIT then print “invalid selection” msg
More practice? • Process characters in String • Use stringVariable.charAt( int pos)& stringVariable.length() • Process digits in an int • Use / & % to split it up • Compute PI & e using sequence • Compute square root (Newton Raphson) • Print table of projectile motion