Start - PowerPoint PPT Presentation

About This Presentation
Title:

Start

Description:

Start More Loops 03/14/11 Look at geometric series example. Other Loops for-loop and do-while for - loop Can use while-loop for all repetition for is useful for ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 15
Provided by: bois59
Learn more at: http://cs.boisestate.edu
Category:
Tags: estate | start

less

Transcript and Presenter's Notes

Title: Start


1
Start
2
More Loops
  • 03/14/11
  • Look at geometric series example.

3
Other Loops
  • for-loop and do-while

4
for - loop
  • Can use while-loop for all repetition
  • for is useful for counting

5
Example
  • //Add first ten integers
  • int total 0
  • for (int n 1 n lt 10 n)?
  • total total n
  • cout ltlt total
  • OUTPUT
  • 55

6
Syntax of for
  • for(intialization repetition cond update)
    statement
  • for(intialization repetition cond update)
  • //Code of for-loop block
  • Initialization done upon entry.
  • Condition must be true, check each time.
  • Update done after each iteration, before
    condition is checked.

7
while vs. for
  • int total 0
  • for (int n 1 n lt 10 n)?
  • total total n
  • total 0
  • int n1
  • while (n lt 10)?
  • total total n
  • n

8
Other Examples
  • Input the rainfall for the week
  • forrain.cpp
  • To find a Fibonacci number.
  • fib.cpp

9
Output Geometric Series
  • A geometric series is defined by
  • a ar ar2 ar3 ... arn-1
  • a is first term, r is common ratio, and n is
    number of terms. Write a function to take a, r
    and n then return the sum.
  • Start with an algorithm

10
Algorithm
  • Parameters a, r, n
  • term a (1st term is a)
  • sum term
  • for(i from 2 to n)
  • Find ith term, termr
  • Add term to sum
  • Output sum

11
Geometric Series Program
  • geometric.cpp
  • Program started.
  • Let's provide the function, using a for-loop.

12
Do-while loop
  • do
  • stmts
  • while (bool. expr.)?
  • Executed Once
  • Then condition check

13
dowhile
  • dowhile.cpp

14
What is needed?
  • Finish for loop in countdown.cpp
Write a Comment
User Comments (0)
About PowerShow.com