Java Coding 3 David Davenport Computer Eng. Dept.,

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Java Programming: From the Ground Up
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Repetition-Sentinel,Flag Loop/Do_While
Lecture 07 More Repetition Richard Gesick.
CSS161: Fundamentals of Computing
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Repetition Statements
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

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

IMPORTANT… Students… Instructors… 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 for ( init; condition; update) statement; Statement to be repeated, input, output, assignment, decision, repetition! Condition exactly as for if statements where statement is any Java statement condition is a boolean expression

The while statement Does statement while condition true does statement 0 or more times statement condition true false while (condition) statement;

If you print out count as well as the asterisk, what do you get? 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”); Use algorithm to begin with. Start with printing a single “*”, add loop and observe need to count number printed so far. Show can print out count too. If you print out count as well as the asterisk, what do you get?

Examples (2) Read & sum 5 values By analogy… 5 3 7 4 1 sum is 20 sum = 0; count = 0; while ( count < 5 ) { value = scan.nextInt(); sum = sum + value; count = count + 1; } System.out.println( “sum is ” + sum); Again begin from algorithm; what do you need to repeat (read value & add to sum), then put in loop & figure out condition Note: should be using named constant to 5 here! Modify to ask user number of values to sum up.

Immediately initialize to 0 (1) before loop 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; } Such design patterns are extremely important. They encode good engineering practice. Remembering/using them in the appropriate places saves time and effort. Immediately initialize to 0 (1) before loop

Generic form of while What happens if you fail to… In general… Initialise any variables in condition while (test condition variable) do statement & update condition variables; 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!

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”); Trace for i = 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, done. At least until quite recently no proof that the example halted, but it always did for every no. tried! Cem Tozer from METU published proof a year or so back. Is it correct? Lots of pages!! Halting problem, a limit to computation! Proof that cannot always write program to determine whether given algorithm will halt or not. (ref. Halting Problem – Alan Turing.)

Reading set of data Three approaches How many? 5 3 5 7 4 1 sum is 20 More? Y 3 5 More? Y 7 More? Y 4 More? Y 1 More? N sum is 20 3 5 7 4 1 -1 sum is 20 Sentinel value non-data value marks end of list Consider advantages/disadvantages of each Sentinel is value that guards/marks the end of the set. non-data value (not always possible to find)

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 Try developing this from beginning. Look at wrong options too! 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.

Java for statements Same as while loop! Use as short-hand counting style loop statement condition true false update init for ( init; condition; update) statement; Notes: variable i is often defined in the for loop; will not be visible outside! Variables only visible inside block defined in. i++ is shorthand notation for i = i + 1; Example: for ( i = 0; i < 5; i = i + 1) System.out.println( “*”);

Java do-while statements Repeat 1 or more times do statement; while (condition); statement condition true false 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 value while value is not positive Read value btw 5 & 10 inclusive. 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): _ Write algorithm for display menu & perform selection tasks Translate into Java. 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 Process digits in an int 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