140 likes | 234 Views
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.
E N D
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 • The number of times the process will be repeated is determined by the programmer when setting the limit.
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.
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
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)
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.
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 ++
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 ++
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 ++
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 ++
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.
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
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.