1 / 14

Introducing Repetition

Introducing Repetition. Repetition Flow Control (Loops). The for statement Causes a set of statements to be repeated a fixed number of times as it counts through a range of numbers up to the limiting value. The for Statement.

marlin
Download Presentation

Introducing Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introducing Repetition

  2. Repetition Flow Control (Loops) • The for statement • Causes a set of statements to be repeated a fixed number of times as it counts through a range of numbers up to the limiting value.

  3. The for Statement • The number of times the process will be repeated is determined by the programmer when setting the limit.

  4. Thefor Statement Structure for (int index = 0; index < limit; index++) { statements to be repeated } • How it works: • int index = 0  declares and initializes a “counting” variable that will keep track of how many times it has gone through the loop. • index < limit  compares the value of index to the value of limit. If index < limit is TRUE, it performs the statements inside the structure. • index ++  increases the counter variable by 1.

  5. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 0

  6. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 0 ? index (0) < limit (3)

  7. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 0 ? index (0) < limit (3) Go TRUE Code to print “Go” on the screen.

  8. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 0 index = 1 ? index (0) < limit (3) ? index (1) < limit (3) Go Code to print “Go” on the screen. index ++

  9. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 1 index = 2 ? index (1) < limit (3) ? index (2) < limit (3) Go TRUE Go Code to print “Go” on the screen. index ++

  10. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 2 index = 3 ? index (2) < limit (3) ? index (3) < limit (3) Go TRUE Go Code to print “Go” on the screen. Go index ++

  11. How the for Statement Works Program flow int limit = 3; // set the # of times to repeat. for (int index = 0; index < limit; index++) { statements to be repeated } index = 3 FALSE ? index (3) < limit (3) Go Go Code to print “Go” on the screen. Go index ++

  12. Nested Loops • In nested loops, an entire loop structure is within another loop structure. • The inner loop will be completely executed each time through the outer loop.

  13. Nested Loops Example for (outerIndex = 1; outerIndex < 3; outerIndex++) { for (innerIndex = 0; innerIndex < 3; innerIndex ++) { System.out.println(outerindex, innerIndex); } } Output: OuterInner 10 11 12 20 21 22

  14. for statement assignment • Using the Alice program you created previously that had an animal of your choice jump realistically, use a for loop structure to have it jump repeatedly. Prompt the user for the number of times it should jump.

More Related