0% found this document useful (0 votes)
26 views

Looping: Instructor: Mr. Neil A. Basabe, MIT

Looping structures like while, for, and do-while loops allow code to be repeatedly executed. The while loop continuously executes as long as a boolean condition is true. The for loop is used when the number of iterations is known, and specifies the initialization, condition, and increment. The do-while loop checks the condition after running the block once. Nested loops contain inner and outer loops that do not overlap.

Uploaded by

Neil Basabe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Looping: Instructor: Mr. Neil A. Basabe, MIT

Looping structures like while, for, and do-while loops allow code to be repeatedly executed. The while loop continuously executes as long as a boolean condition is true. The for loop is used when the number of iterations is known, and specifies the initialization, condition, and increment. The do-while loop checks the condition after running the block once. Nested loops contain inner and outer loops that do not overlap.

Uploaded by

Neil Basabe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Looping

Instructor: Mr. Neil A. Basabe, MIT


while Loops
while loop
Executes a body of statements continually
As long as the Boolean expression that controls entry into the loop continues to be
true
Consists of:
The keyword while
Followed by a Boolean expression within parentheses
Followed by the body of the loop; can be a single statement or a block of
statements surrounded by curly braces
Definite loop (counted loop): programmer knows exact number of iterations
Indefinite loop: programmer cannot predict number of iterations
BankBalance application example using while loop
for Loop
• for loop
• Used when a definite number of loop iterations is required
• One convenient statement indicates:
• The starting value for the loop control variable
• The test condition that controls loop entry
• The expression that alters the loop control variable
• Unconventional for loops
• Initialization of more than one variable
• Place commas between separate statements
• Performance of more than one test using AND or OR operators
• Decrementing or performance of some other task
• Altering more than one value

• You can leave one or more portions of a for loop empty


• Two semicolons are still required as placeholders

• 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 ();

You might also like