Looping: Instructor: Mr. Neil A. Basabe, MIT
Looping: Instructor: Mr. Neil A. Basabe, MIT
• Use the same loop control variable in all three parts of a for statement
• To pause a program:
• Use the for loop that contains no body (do-nothing loop)
• for(x = 0; x < 100000; ++x);
• Or use the built-in sleep() method
do…while Loop
• do…while loop
• As a pretest loop:
• Checks the value of the
loop control variable
before loop body
• As a posttest loop
• Checks the value of the
loop control variable
• At the bottom of the loop
• After one repetition has
occurred
• Performs a task at least
one time
• You are never required to
use this type of loop
• Use curly braces to block
the statement
• Even with a single
statement
BankBalance application example using do while loop
Nested Loops
• Inner loop and outer loop
• An inner loop must be entirely contained in an outer loop
• Loops can never overlap
• To print three mailing labels for each of 20 customers:
• for(customer = 1; customer <= 20; ++customer)
• for(color = 1; color <= 3; ++color)
• outputLabel ();