DOC PREVIEW
Duke CPS 004 - Looping Structures

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Looping StructuresThe PlanMotivationSlide 4for LoopSlide 6Slide 7Slide 8while LoopSlide 10Slide 11do-while LoopSlide 13Slide 14Equivalence of LoopsWhen to use which loop?Slide 17Application of Simulated CollisionSlide 19Slide 20Slide 21Practice ProblemsDesign of Video Game PackageSlide 24Basic Principles of DesignWhy Simple?Why functional?Why fast?Why correct?Why easy to modify?Why easy to extend?Capable of ReuseWhy we need MVC?What is MVC?Simplified 1D ModelWhat are our objects?class Particleclass Segment extends Particleinterface Tracker1Dclass VelocityTrackerclass ModelTestMore to come….Looping StructuresThe PlanWhile not everyone understands:1. Motivate loops2. For loops3. While loops4. Do-while loops5. Equivalence6. Application of Simulated Collision7. Practice ProblemsMotivationWhy loop?Sometimes you need to do things again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again...and finally you get tired of typing.MotivationOkay, so that's not all. You also loop in order to:●Group repeatedly executed code for uniformity●Make the number of repetitions easily changeable●Repeat events which the number of exectutions is known only dynamically●Combine with selection statements to make more complex algorithmsfor Loopfor(int i=0; i<10; i++){System.out.println(i);}for Loopfor(int i=0; i<10; i++){System.out.println(i);}InitializationConditionUpdateTrueFalsefor Loopfor(int i=0; i<10; i++){System.out.println(i*0.1);}InitializationConditionUpdateTrueFalseScale factorfor Loopfor(int i=0; i<10; i++){System.out.println(i*0.1+5);}InitializationConditionUpdateTrueFalseScale factorTranslationwhile Loopint i=0;while(i<10){System.out.println(i);i++;}while Loopint i=0;while(i<10){System.out.println(i);i++;}InitializationConditionUpdatetruewhile Loopdouble i=0;while(i<1){System.out.println(i);i+=0.1;}Why mightthis be a problem?do-while Loopint i=0;do{System.out.println(i);i++;}while(i<=10);do-while Loopint i=0;do{System.out.println(i);i++;}while(i<=10);InitializationConditionUpdateTrueFalsedo-while Loopint i=0;do{System.out.println(i);i++;}while(i<=10);Notice this semicolonwas not here in the while loop!int i=0;while(i<10){System.out.println(i);i++;}Equivalence of Loopsfor(int i=0; i<10; i++){System.out.println(i);}When to use which loop?Is it known how many times the loop will execute prior to executing the loop body?YesforNoIs it important for the loop body to always execute at least once?Yesdo-whileNowhileWhen to use which loop?Real answer: Use which ever structure is most convenient, because all loop structures can be represented as any other loop structure.Why are there multiple loop structures then?Simple answer – for the programmer’s convenience.Application of Simulated Collisiondouble velocity=3;double position=1;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(time<5){System.out.println("("+t+", "+position+")");time+=timeStep;}Application of Simulated Collisiondouble velocity0=3; double velocity1=-2;double position0=1; double position1=10;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(position0!=position1){System.out.println(“p0 is (“+t+", "+position0+")");System.out.println(“p1 is (“+t+", "+position1+")");time+=timeStep;}What’s wrong?Why doesn’t theprogram end?Application of Simulated Collisiondouble velocity0=3; double velocity1=-2;double position0=1; double position1=10;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(position0<position1){System.out.println(“p0 is (“+t+", "+position0+")");System.out.println(“p1 is (“+t+", "+position1+")");time+=timeStep;}Problem fixed,right?Application of Simulated Collisiondouble velocity0=-3; double velocity1=2;double position0=10; double position1=1;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(position0<position1){System.out.println(“p0 is (“+t+", "+position0+")");System.out.println(“p1 is (“+t+", "+position1+")");time+=timeStep;}What about now?(notice velocity and position change)Practice Problems●Write a loop to print out from 10 to 100 inclusive counting by 10s●Write a loop that starts with an arbitrary double x and divides it by 2 repeatedly until it is less than 1. Output the number of times the loop executed. What is being computed?●Write a loop that sums the first x integers where x is a positive integer. Print out the results.●Write a loop that takes an integer x starting with value 1 and doubles x so long as x is positive. Bonus question: why doesn’t this loop infinitely? Super Bonus question: why does it loop infinitely when x is a double?Design of Video Game PackageAnimationCanvasKeyboardMouseGameLoopJFrameOrAppletTrackerSpriteTrackerSpriteTrackerSpriteThe Plan●Basic principles of design●Motivate why MVC is needed●Model/View/Controller (MVC) overview●Model (today)●View (when we get to Graphical User Interfaces)●Controller (when we get to Event Handling)Basic Principles of DesignWe want our programs to be●simple●functional●fast (to code and to execute)●correct●easy to modify●easy to extend●capable of reuseWhy Simple?●Simple is easy to understand and therefore easier to reuse, modify, and extend.●Simple has less likelihood for program errors and is therefore more likely to be correct.●Simple may be faster than complex, certain to code and perhaps in execution time.Why functional?●It has to work.●How well it works is negotiable considering:–speed to release–cost to release–lifetime of codeWhy fast?●Who wants to wait?●Works better on slower, more out-of-date machines.●Sometimes crucial to application’s purpose–air control–nuclear plant facilities–weather prediction–stock predictionWhy correct?●In some cases, incorrect execution is unacceptable–access control–online sales●In some cases, incorrect execution can be remedied by re-execution–operating system locks up, so just reboot–database goes down, just restart●impacts time codingWhy easy to modify?●Users rarely can tell you exactly what they want●Even if they could tell you, what users want changes●Changes in hardware and software mandate code modification (think Y2K)Why easy


View Full Document

Duke CPS 004 - Looping Structures

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

32 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Looping Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Looping Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Looping Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?