Module 5 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Module 5

Description:

It lets us do one (or more) things many times. Two types of loops: Count controlled loops: do x number of times ... Variables only hang around as long as we need them ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 23
Provided by: humb
Category:
Tags: hang | module

less

Transcript and Presenter's Notes

Title: Module 5


1
Module 5
  • Loops

2
What is a loop?
  • A loop is a repetition control structure
  • It lets us do one (or more) things many times
  • Two types of loops
  • Count controlled loops do x number of times
  • Event controlled loops do until some condition
    is true
  • For loops, many rules similar to those you
    learned for the if-else statement

3
The while loop
while (expression) // single statement
form statement1 while(expression) // many
statement block statement1 statement2 stat
ement3
4
Some Simple while Rules
  • The expression is evaluated to zero or nonzero
    (true or false if you use Boolean)
  • The contents are done while expression evaluates
    to true
  • Otherwise we go on and execute the next thing
    following the while
  • (Sound familiar?)

5
Count-Controlled Loop
int count count 4 while (count gt 0) cout
ltlt count-- ltlt endl cout ltlt Done with count
value ltlt count
6
Note On Variables and Blocks
int count count 4 while (count gt
0) cout ltlt count-- ltlt endl cout ltlt
Done with count value ltlt count
// This last line now produces an error!
7
Variables and Scope
  • Variables only hang around as long as we need
    them
  • We demark where they are needed by the blocks we
    createvariables dont persist once the block is
    ended (why?)
  • Where the variable is useable (readable and
    writeable) is called the variables scope

8
Count-Controlled Loop Example
int thisNum // current number
input by user int total 0 // running total of
input values int count 0 while (count lt 100)
// How many times will this execute? cout ltlt
Please input a number ltlt endl cin gtgt
thisNum total total thisNum count cou
t ltlt The total is ltlt total ltlt endl
9
Event-Controlled Loop Example
int thisNum 1 // current number input
by user int total 0 // running total of input
values while (thisNum) // How many times will
this execute? cout ltlt Please input a number
ltlt endl cin gtgt thisNum total total
thisNum cout ltlt The total is ltlt total ltlt
endl
10
Important Note About Loops
  • Here I deliberately used a while loop for the
    count-controlled loopit required extra
    statements
  • There is another loop, called a for loop, that is
    generally better for count-controlled loops
  • Thus, each type of loop has a particular purpose
    for which it is intended!

11
Important Note, Continued
  • Note that the while loop does its test at the
    start of the loop
  • There is another loop called a do-while loop
    which does its test at the end of the loop
  • Thus, again, each type of loop has a particular
    purpose for which it is intended!
  • If you choose wisely, your code is more readable,
    debugable, and maintainable

12
Loops Compared
Loop Type Loop Test Placement
Loop Condition for loop at beginning
fixed of times while loop at beginning
test a value do-while loop at end test a
value
13
Which Type of Loop is Appropriate?
  • If I want to check successive inputs from a user
    to see if the value is zero?
  • If I want to compare a name I have to every name
    in a list of 20 other names?
  • If I want to divide by the variable A but I need
    to make sure A is not zero the division changes
    the value of A?

14
The for loop
for (initialize test increment) statementA f
or (initialize test increment)
15
A for-loop example
int count, countFactorial 1 //
Acquire value from user cout ltlt Please enter a
positive integer cin gtgt count // Calculate
factorial of input value for ( count gt 1
count count - 1) countFactorial count //
Output users value and its factorial cout ltlt
count ltlt ! ltlt countFactorial ltlt endl
16
for loop execution path
  • When entering the loop, the initialization is
    executed first, then the test
  • if test is false here, we skip the loop!
  • After that, we execute the body, then increment,
    then test until the test is false
  • It is possible that some of these items will be
    null statements
  • Take time to learn the order of execution
    described hereit will eliminate common errors
    with for loops!

17
What gets printed?
for (int j 1 j lt 100 j) cout ltlt j ltlt
// this is a typical loop structure
18
What gets printed?
int k for (k 34 k 0 ) // note the
third item here! if ((k 5) 0) cout ltlt
k-- cout ltlt k ltlt ltlt endl
19
The do-while loop
do statementA while(expression) do statemen
tA statementB while(expression)
20
Example do-while
do cin gtgt response if ((response ! y)
(response ! n)) cout ltlt Please type y or n
while((response ! y) (response !
n))
21
Equivalence of loops
  • In some sense, these three loop structures are
    equivalent that is, I can simulate any one of
    them with one of the others
  • We have three different ones because they make it
    possible for us to do things in ways that are
    more natural to read

22
Example of Equivalence
for (j 0 j lt 10 j) statementA //
equivalent while j 0 while(j lt
10) statementA j // can you create
the equivalent do-while?
Write a Comment
User Comments (0)
About PowerShow.com