Ch6Iterations - Chapter 6 Iteration/Loops Investment with...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6 Iteration / Loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Investment with Compound Interest Year Balance 0 $10,000 1 $10,500 2 $11,025 3 $11,576.25 Invest $10,000, 5% interest, compounded annually When will the balance be at least $20,000?
Background image of page 2
Iterations It is essential that a program be able  to execute the  same set of  instructions   many times : otherwise a  program would do only as much  work as a programmer! Repeating the same code fragment  several times is called  iterating .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Loops A block of code is in { }, we use a block  because we most often want to repeat  more than one statement. A termination condition controls how  often this loop is executed  The condition must evaluate to TRUE or  FALSE – use relational and/or logical  operators. Java provides three control statements  for iterations (a.k.a.  loops ):  for while , and           do-while .
Background image of page 4
The Loop statements Initialization :  the variables tested in the  condition must be initialized to some  values.  If the condition is false at the  outset, the loop is never entered. Test :  the condition is tested before  each  iteration.  If false, the program continues  with the first statement after the loop. Increment (change) :  At least one of the  variables tested in the condition must  change in the body of the loop.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
For loops Most common loop used. Used when we know exactly how  many times to perform the loop. Used with a constant  increment/decrement. Easiest because the initial value,  test, and increment/decrement are  all located in the parentheses.
Background image of page 6
Syntax 6.2. The for Statement for ( initialization condition update  ) statement     int i = 0; for ( i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; Purpose: To execute an initialization, then keep  executing a statement and updating an 
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
for Statement for ( initialization condition update )    statement   Int i = 0;  for (i = 1; i <= n; i++) {   double interest = balance * rate / 100; balance = balance + interest; Equivalent to initialization ; while ( condition ) statement update ; }
Background image of page 8
Variations on the for Loop Counting DOWN for(int i = 5; i>=0; i--) // do something Counting by SOMETHING ELSE for(int i = 0; i< 100; i= i + 2) // do something OR for(int i = 0; i< 100; I += 2) // do something
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
for Loop
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/28/2010 for the course COMP COMP taught by Professor Huye during the Spring '10 term at Eastern Oregon.

Page1 / 43

Ch6Iterations - Chapter 6 Iteration/Loops Investment with...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online