Loops - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Loops

Description:

... enters the letter y. As soon as another letter is entered ... means less than or equal to -- while(x =5) = means greater than or equal to -- while(x =0) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 15
Provided by: stude70
Category:
Tags: letter | loops

less

Transcript and Presenter's Notes

Title: Loops


1
Loops
  • Chapter 3

2
In this chapter
  • Flowcharts
  • The difference between pre test and post test
    loops
  • How to use relational operators in loop
    conditions
  • Constructing counter controlled loops
  • Constructing sentinel controlled loops
  • Some applications of loops, including data input
    and validation, and computing sums and averages
  • Using nested loops

3
flowcharts
  • A flowchart is a diagram that uses special
    symbols to display pictorially the flow of
    execution within a program or program module.

4
Loop
  • A loop is a repetition structure. A loop repeats
    a segment of code. The number of times the code
    segment is repeated is dependent on the exit
    condition. The exit condition can be at the
    beginning or end of the loop, dependant on the
    logic of how you want the loop to operate in
    relation to the exit condition. We will write
    three types of loops, the while, do-while, and
    for loop.

5
while loop
  • int x 0
  • while(xlt10) /exit condition/
  • coutltltJim is my hero.ltltendl
  • xx1 /counter adds to the value of x/
  • The while loop is an all purpose loop. The exit
    condition is at the beginning, making this a
    pre-test loop. This loop is a counter controlled
    loop. If the counter (xx1)is missing the loop
    runs indefinitely because the value of x is never
    changed so the exit condition is never false.

6
do-while loop
  • int x0
  • do
  • coutltltJim is my hero.ltltendl
  • xx1/counteradds to the value of x/
  • while(xlt10)
  • The do while loop is a post test loop because the
    exit condition is at the end. Notice the
    semicolon after the exit condition. This is the
    only loop that is written with a semicolon. This
    is also a counter controlled loop. A do while
    loop will always run at least one time. This loop
    works well with menus.

7
for loop
  • int i0
  • for(i0ilt10i)
  • coutltltJim is my hero.ltltendl
  • /end for loop/
  • The for loop is another counter controlled loop.
    Inside the parenthesis the i0 part gives the
    variable i an initial value of 0 The ilt10 part
    is the exit condition The i part is shorthand
    for ii1. This is called incrementing the
    variable. The variable is incremented at the
    closing curly brace. are curly braces.

8
sentinel controlled loop
  • char passy
  • while (passy)/ is a test for the same
    as/
  • coutltltJim is my hero.ltltendl
  • coutltltEnter y to continue or any key to
    end.ltltendl
  • cingtgtpass)/store response from user/
  • This loop repeats as long as the user enters the
    letter y. As soon as another letter is entered
    the loop ends.

9
do-while with sentinel value
  • char passx
  • do
  • coutltltJim is my hero.ltltendl
  • coutltltEnter y to continue or any key to
    end.ltltendl
  • cingtgtpass)/store response from user/
  • while (passy)
  • The exit condition is at the end so this loop is
    guaranteed to run at least once. The loop is
    continued or ended by the actions of the user.

10
exit condition
  • The exit conditions determine when a loop stops.
    There are six basic relational operators that we
    use in the exit condition
  • means the same as -- while(passy)
  • lt means less than -- while(xlt5)
  • gt means greater than -- while(xgt0)
  • ! means not equal to -- while(pass !q)
  • lt means less than or equal to -- while(xlt5)
  • gt means greater than or equal to -- while(xgt0)

11
nested for loops
  • /The first column you see in the output is
    produced by the outer for loop.All
  • other numbers come from the inner loop./
  • includeltiostreamgt
  • includeltstringgt
  • using namespace std
  • main()
  • int i0
  • int j0
  • for(i0ilt10i)
  • printf("d ",i)
  • for(j0jlt5j)
  • printf("d ",j)
  • / end for(j/
  • coutltltendl
  • /end for(i/
  • return 0

12
data validation
  • We can validate the data by using it in our exit
    condition. What if we wrote a program that
    calculates the sum of an undetermined amount of
    numbers?

13
if statement nested in a loop
  • includeltiostreamgt
  • using namespace std
  • main()
  • int sum0
  • int num0
  • char pass'y'
  • do
  • if(numlt0)
  • coutltlt"Enter a positive number to be
    summed."ltltendl
  • cingtgtnum
  • coutltlt"You entered " ltltnumltltendl
  • if(numlt0)
  • coutltlt"This number will not be added to the
    total"ltltendl
  • num0

14
Output from previous slide
Write a Comment
User Comments (0)
About PowerShow.com