ch05_1 - for repetition statement dowhile repetition...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical operators Control statements summary.
Background image of page 2
Counter-controlled repetition requires a control variable (or loop counter) the initial value of the control variable the increment (or decrement ) by which the control variable is modified each time through the loop (also known as each iteration of the loop ) the loop-continuation condition that determines if looping should continue.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
In Fig. 5.1, the elements of counter-controlled repetition are defined in lines 8, 10 and 13. Line 8 declares the control variable ( counter ) as an int , reserves space for it in memory and sets its initial value to 1 . The loop-continuation condition in the while (line 10) tests whether the value of the control variable is less than or equal to 10 (the final value for which the condition is true ). Line 13 increments the control variable by 1 for each iteration 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 repetition statement Specifies the counter-controlled-repetition details in a single line of code. Figure 5.2 reimplements the application of Fig. 5.1 using for .
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
The general format of the for statement is for  (  initialization loopContinuationCondition increment  )      statement the initialization expression names the loop’s control variable and optionally provides its initial value loopContinuationCondition determines whether the loop should continue executing increment modifies the control variable’s value (possibly an increment or decrement), so that the loop-continuation condition eventually becomes false. The two semicolons in the for header are required.
Background image of page 9

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

View Full DocumentRight Arrow Icon
a)Vary the control variable from 1 to 100 in increments of 1 . for  (  int  i =  1 ; i <=  100 ; i++ ) b)Vary the control variable from 100 to 1 in decrements of 1 . for  (  int  i =  100 ; i >=  1 ; i-- ) c)Vary the control variable from 7 to 77 in increments of 7 . for  (  int  i =  7 ; i <=  77 ; i +=  7  )
Background image of page 10
d)Vary the control variable from 20 to 2 in decrements of 2 . for  (  int  i =  20 ; i >=  2 ; i -=  2  ) e)Vary the control variable over the values 2 , 5 , 8 , 11 , 14 , 17 , 20 . for  (  int  i =  2 ; i <=  20 ; i +=  3  ) f)Vary the control variable over the values 99 , 88 , 77 , 66 , 55 , 44 , 33 , 22 , 11 , 0 . for  (  int  i =  99 ; i >=  0 ; i -=  11  )
Background image of page 11

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

View Full DocumentRight Arrow Icon
Background image of page 12
Compound interest application A person invests $1000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/06/2010 for the course CS 2050 taught by Professor Uhlmann during the Fall '09 term at Missouri (Mizzou).

Page1 / 56

ch05_1 - for repetition statement dowhile repetition...

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

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