Control structure - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Control structure

Description:

... class-average program with a technique called top-down , ... We begin with a pseudocode representation of the top : Determine the class average for the quiz. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 23
Provided by: alich6
Category:

less

Transcript and Presenter's Notes

Title: Control structure


1
chapter2
  • Control structure

2
objectives
  • To understand basic problem-solving techniques.
  • To be able to use the if, if/else and switch
    selection structures to choose among alternative
    actions.
  • To be able to use the while, do/while and for
    repetition structures to execute statement in a
    program repeatedly.
  • To understand counter-controlled repetition and
    sentinel-controlled repetition.
  • To be able to use the increment and logical
    operators.
  • To be able to use the break and continue program
    control statements.

3
If/else selection structure
  • The if selection structure performs an indicated
    action only when the condition is true, otherwise
    the action is skipped.
  • If (BOOLEAN STATEMENT)
  • IF_STATEMENT
  • If (grade gt 60) cout ltlt passed
  • If (BOOLEAN STATEMENT)
  • IF_STATEMENT
  • else
  • ELSE_STATEMENT

4
If/else selection structure
  • If (grade gt 60) cout ltlt passed
  • else cout ltlt failed
  • cout ltlt (grade gt 60 ? passed failed)
  • if (grade gt 90) cout ltlt A
  • else if (grade gt 80) cout ltlt B
  • else if (grade gt 70) cout ltlt C
  • else if (grade gt 60) cout ltlt D
  • else cout ltlt F

5
while repetition structure
  • A repetition structure allows the programmer to
    specify that a program should repeat an action
    while some condition remains true.
  • While there are more items on my shopping list
  • purchase next item and cross it off my list.
  • int product 2
  • while (product lt 1000)
  • product 2 product //product 2

6
Counter control repetition
  • include ltiostream.hgt
  • int main()
  • int total 0, gradeCounter 1, grade, average
  • while( gradeCounter lt 10)
  • cout ltlt Enter grade
  • cin gtgt grade
  • total grade
  • gradeCounter
  • average total / 10
  • cout ltlt class average is ltlt average ltlt endl
  • return 0

7
Analyze the program
  • Line 3 is declaration and initialization.
  • Line 4 indicate that the while structure should
    continue as long as gradeCounters value is less
    than or equal to 10.
  • Line 5, 6 means input the next grade
  • Line 7 adds grades to the previous value of
    total and assign the result to total.
  • Line 8 adds 1 to gradeCounter.
  • Line 10 assign the result of the average
    calculation to variable average.

8
Analyze the program
  • Line 11 prints class average is followed by
    the value of variable average

9
Formulating Algorithms with top-down, stepwise
refinement
  • Case study (Sentinel-controlled Repetition)
  • Consider the following problem
  • Develop a class-averaging program that will
    process an arbitrary number of grades each time
    the program is run.

10
Case study
  • In this example, no indication is given of how
    many grades the user will enter during the
    programs execution.
  • How can the program determine when to stop the
    input of grades?
  • How will it know when to calculate and print the
    class average?
  • One way is sentinel value (flag value) to
    indicate end of data entry .

11
Case study
  • We approach the class-average program with a
    technique called top-down , stepwise refinement.
  • We begin with a pseudocode representation of the
    top
  • Determine the class average for the quiz.
  • We divide the top into series of smaller tasks
    and list these in the order in which they need to
    be performed.

12
Case study
  • Initialize variables
  • Input , sum and count the quiz grades.
  • Calculate and print the class average
  • This refinement uses only the sequence
    structure--the steps listed should execute in
    order, one after the other.
  • In this example, we need a running total of the
    numbers, a count of how many numbers have been
    processed, a variable to receive the value of
    each grade as it is input by the user and a
    variable to hold the calculated average.

13
Case study
  • The pseudocode statement
  • Initialize variables
  • Can be refined as follows
  • Initialize total to zero
  • Initialize counter to zero
  • Dont need to initialize the average and X
    variables.

14
Case study
  • The pseudocode statement
  • Input , sum and count the quiz grades
  • requires a repetition structure that successively
    inputs each grades.
  • The second refinement of the preceding pseudocode
    statement is then
  • Input the first grade (possibly the sentinel)
  • While the user has not yet entered the sentinel
  • Add this grade into the running total
  • Add one to the grade counter
  • Input the next grade (possibly the sentinel)

15
Case study
  • The pseudocode statement
  • Calculate and print the class average
  • Can be refined as follows
  • If the counter is not equal to zero
  • Set the average to the total divided by the
    counter
  • Print the average
  • else
  • print No grade was entered

16
Case study
  • Note that we are being careful here to test for
    the possibility of division by zero-normally a
    fatal error that, if undetected, would cause the
    program to fail (often called bombing or
    crashing).

17
Case study
  • Initialize total to zero
  • Initialize counter to zero
  • Input the first grade (possibly the sentinel)
  • While the user has not yet entered the sentinel
  • Add this grade to the running total
  • Add one to the grade counter
  • Input the next grade (possibly the
    sentinel)

18
Case study
  • If the counter is not equal to zero
  • set the average to the total divided by the
    counter
  • Print the average
  • Else
  • Print No grades were entered
  • This algorithm was developed after only two
    levels of refinement. Sometimes more levels are
    necessary.

19
Write program
  • // class average program with sentinel-controlled
    repetition.
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • int main()
  • int total 0
  • int gradeCouner 0
  • int grade
  • double average

20
Write program
  • cout ltlt Enter grade, -1 to end
  • cin gtgt grade
  • while ( grade ! -1)
  • total total grade
  • gradeCounter gradeCounter 1
  • cout ltlt Enter grade, -1 to end
  • cin gtgt grade

21
Write program
  • if ( gradeCounter ! 0)
  • averagestatic_castltdoublegt(total)/
    gradeCounter
  • coutltltClass average is ltltsetprecision(2)
    ltlt fixed ltlt average ltlt endl
  • else
  • cout ltlt No grades were entered ltlt endl
  • return 0

22
A sample execution
  • Enter grade, -1 to end 23
  • Enter grade, -1 to end 43
  • Enter grade, -1 to end 64
  • Enter grade, -1 to end 56
  • Enter grade, -1 to end 78
  • Enter grade, -1 to end 89
  • Enter grade, -1 to end -1
  • Class average is 58.83
Write a Comment
User Comments (0)
About PowerShow.com