Complex Selections and Repetitions - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Complex Selections and Repetitions

Description:

Chapter 9 Complex Selections and Repetitions 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection. – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 27
Provided by: gary213
Category:

less

Transcript and Presenter's Notes

Title: Complex Selections and Repetitions


1
Chapter 9
  • Complex Selections and Repetitions

2
  • 9.1 INTRODUCTION
  • Then we introduce the switch statement, which can
    also be used for multiway selection.
  • For statement, which is another pretest
    repetition structure.
  • Do-while statement. The do-while statement is the
    C implementation of the posttest repetition
    structure.

3
  • 9.2 COMPLEX PREDICATES
  • Logical Expressions and Logical Operatiors
  • Logical and (conjunction)
  • Logical or (disjunction)
  • Logical not (negation)
  • The C Logical Operators
  • Logical and is represented by .
  • The symbol stands for logical or.
  • The C logical not operator is !.
  • Example 9.1
  • (semester_average gt 80 )
    (semester_average lt 89)

4
  • Example 9.3
  • ! (student_status u)
  • Simplifying Complex Predicates
  • in some selection problems there are several
    forms for expressing the same condition.
  • We should try to get rid of the negation and
    find the simplest form in order to enhance the
    readability of the code.

5
  • Predicate Equivalent Simple Form
  • ! (a b) a ! b
  • ! (a lt b) a gt b
  • ! (a gt b) a lt b
  • ! (Expression_1 Expression_2) (!
    Expression_1) (Expression_2)
  • Figure 9.1 Equivalent forms for predicates
    involving negation

6
  • Precedence of Logical Operators
  • Logical not , unary arithmetic operators
  • Binary arithmetic operators
  • Relational operators
  • Logical and
  • Logical or
  • Example 9.6
  • xy gt 13 ! (x-y) xy -16 4
  • 4 6 8 2 1 9 3 5 7

7
  • 9.3 MULTIWAY SELECTION USING THE switch AND
    break STATEMENTS
  • switch (ControllingExpression)
  • CaseClause-1
  • CaseClause-2
  • .
  • .
  • CaseClause-n
  • DefaultClause
  • / end switch /

8
  • In the switch statement the controlling
    expression is evaluated first. The controlling
    expression must compute to an integral value and
    must be of type int or char
  • If the value of the controlling expression does
    not match any of the constant values in the case
    clauses, the content of the default clause is
    executed.

9
  • switch (major_code)
  • case 1
  • printf(student major is computer science.
  • break
  • case 7
  • printf(Student major is computer
    engineering.
  • break
  • default
  • printf(Student major is a noncomputer
    field.)
  • / end switch /

10
  • If the actions of two or more consecutive cases
    are identical, all we have to do is list the
    cases with empty statements and specify the
    action and a break statement in the final case.

11
  • switch (character)
  • case 0 case 1 case 2 case 3
    case 4 case 5 case 6 case 7
  • case 8 case 9
  • printf(The content is a decimal
    integer.)
  • break
  • default
  • printf(The content is a nondecimal
    character.)
  • / end swithc /

12
  • 9.4 STYLE CONSIDERATIONS FOR MULTIWAY SELECTION
    STRUCTURES
  • Keep the use of logical negation to a minimum in
    forming complex predicates.
  • Remember that a nested if statement is more
    general than a switch statement.
  • Use proper indentations in forming switch
    statements.
  • Whenever possible, use a default clause in your
    switch statements.

13
  • 9.5 THE PRETEST REPETITION STRUCTURE
  • The for Statement for Pretest Repetition
  • for (InitializationExpression
    LoopControExpression UpdateExpression)
  • Loopbady
  • / end for /

14
  • Executes the InitializationExpression.
  • Evaluates the LoopControlExpression. If it
    computes to zero, the loop is exited.
  • If the LoopControlExpression yields a nonzero
    value, the LoopBody is executed and then the
    UpdateExpression is evaluated.
  • Tests the LoopControlExpression again. Thus the
    LoopBody is repeated until the LoopControlExpressi
    on computes to a zero value.

15
  • int number, sum 0 , counter 1
  • .
  • .
  • While (counter lt number)
  • sum sum counter
  • counter counter 1
  • / end while /

16
  • int number , sum , counter
  • ..
  • ..
  • Sum 0
  • for (counter 1 counter lt number
    counter counter 1)
  • sum sum counter
  • / end for /

17
  • Equivalence of for and while Statements
  • the same repetition problem can be expressed
    equivalently using either a while or a for
    statement.
  • Using the for Statement for Counter-Controlled
    Loops
  • Using the for Statement for Sentinel-Controlled
    Loops

18
  • char answer
  • printf(Do you want to continue? (y/n))
  • for (scanf(c, answer)
  • answer ! y answer ! Y answer !
    n answer ! N
  • scanf(c , answer))
  • printf(Please type y or n )
  • / end for /

19
  • Checking for Incorrect Data in a Loop and the
    continue Statement
  • we may prefer to warn the user and skip the rest
    of the loop body. Causes the program control to
    skip the rest of the loop body and execute the
    loop again.

20
  • for (scanf(d, test_score) test_score !0
  • scanf(d, test_score))
  • if (test_score lt0 test_score gt100)
  • printf(Incorrect test score ! Enter a
    correct value )
  • continue
  • / end if /
  • sum sum test_score
  • number_of_students number_of students 1
  • printf(Enter a test score. Enter 0 to stop
    )
  • / end for /

21
  • 9.6 THE POSTTEST REPETITION STRUCTURE
  • the loop body is executed before the loop
    control expression is tested
  • The do-while Statement
  • It executes the LoopBody.
  • It evaluates the LoopControlExpression. If the
    value of the LoopControlEcpression is 0, the
    computer exits the loop otherwise, it does the
    LoopBody again.

22
  • int number, sum 0 , counter 0
  • .
  • .
  • Do
  • sum sum counter
  • counter counter 1
  • while (counter lt number)
  • / end do-while /
  • Use of do-while for Counter- and
    Sentinel-Controlled Loops

23
  • 9.7 NESTED LOOPS
  • A nested loop is a repetition structure that
    contains one or more loops in its body. A loop
    contained in another loop forms a doubly nested
    loop.
  • int control_var1, control_var2
  • for (control_var11 control_var1lt8
    control_var1 2)
  • for (control_var2 control_var1 control_var2
    lt 10
  • control_var2 3 )
  • printf(control_var1 d control_var2
    d\n ,
  • control_var1, control_var2)
  • / end for /
  • /end for /

24
  • control_var1 1 control_var2 1
  • control_var1 1 control_var2 4
  • control_var1 1 control_var2 7
  • control_var1 1 control_var2 10
  • control_var1 3 control_var2 3
  • control_var1 3 control_var2 6
  • control_var1 3 control_var2 9
  • control_var1 5 control_var2 5
  • control_var1 5 control_var2 8
  • control_var1 7 control_var2 7
  • control_var1 7 control_var2 10

25
  • 9.8 STYLE CONSIDERATIONS FOR REPETITION
    STATEMENTS
  • Indent the loop body of for statements
  • Avoid the following permitted syntax elements of
    the for statement missing initialization,
    update, and/or loop control expressions in the
    header.
  • Avoid using while and for statements for the
    implementation of posttest repetition structures.
  • Indent the loop body of do-while

26
  • 9.9 EXAMPLE PROGRAM 1 A C Program that Computes
    Distribution of Letter Grades
Write a Comment
User Comments (0)
About PowerShow.com