Break and continue statements Array Examples - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Break and continue statements Array Examples

Description:

break. Causes immediate exit from a while, for, do...while or switch statement ... Common uses of the break statement. Escape early from a loop ... – PowerPoint PPT presentation

Number of Views:131
Avg rating:3.0/5.0
Slides: 25
Provided by: xiaow
Category:

less

Transcript and Presenter's Notes

Title: Break and continue statements Array Examples


1
Lecture 10
  • Break and continue statements Array Examples
  • Reference Section 4.9, 4.12, 6.4

2
break and continue Statements
  • break and continue statements are used to alter
    the flow of control.
  • break
  • Causes immediate exit from a while, for, dowhile
    or switch statement
  • Program execution continues with the first
    statement after the structure
  • Common uses of the break statement
  • Escape early from a loop
  • Skip the remainder of a switch statement

3
(No Transcript)
4
break and continue Statements
  • continue
  • Skips the remaining statements in the body of a
    while, for or dowhile statement
  • Proceeds with the next iteration of the loop
  • while and dowhile
  • Loop-continuation test is evaluated immediately
    after the continue statement is executed
  • for
  • Increment expression is executed, then the
    loop-continuation test is evaluated

5
(No Transcript)
6
Summary of Structured Programming
  • Structured programming promotes simplicity
  • Only three forms of control are needed
  • Sequence
  • Selection
  • if
  • if else
  • switch
  • Repetition
  • while
  • do while
  • for

7
Summary of Structured Programming
8
Summary of Structured Programming
9
Rules for Forming Structured Programs
  • Begin with the simplest flowchart
  • Any rectangle (action) can be replaced by two
    rectangles (actions) in sequence
  • Stacking rule
  • Any rectangle (action) can be replaced by any
    control statement (sequence, if, ifelse, switch,
    while, do .. While, or for)
  • Nesting rule
  • Rules 2 and 3 may be applied as often as you like
    and in any order

10
Rules for Forming Structured Programs
Simplest flowchart
11
Rules for Forming Structured Programs
Repeatedly applying rule 2 (stacking rule) to the
simplest flowchart
12
Rules for Forming Structured Programs
Applying rule 3 (nesting rule) to the simplest
flowchart.
13
Array Examples
  • Initializers
  • int n 5 1, 2, 3, 4, 5
  • If not enough initializers, rightmost elements
    become 0
  • int n 5 0
  • All elements are 0
  • If too many initializers, a syntax error occurs
  • C arrays have no bounds checking
  • If size omitted, initializers determine it
  • int n 1, 2, 3, 4, 5
  • 5 initializers, therefore 5 element array

14
Outline
  • fig06_03.c

15
Outline
  • fig06_04.c

16
Common Programming Errors
  • Forgetting to initialize the elements of an array
    whose elements should be initialized.
  • Providing more initializers in an
    arrayinitializer list than there are elementsin
    the array is a syntax error.

17
Outline
  • fig06_05.c

Element Value 0 2 1
4 2 6 3
8 4 10 5 12
6 14 7 16 8
18 9 20
18
Outline
  • fig06_06.c

19
Outline
  • fig06_07.c
  • (1 of 2 )

20
Outline
  • fig06_07.c
  • (2 of 2 )

21
Error-Prevention Tips
  • When looping through an array, the array
    subscript should never go below 0 and should
    always be less than the total number of elements
    in the array (size 1). Make sure the
    loop-terminating condition prevents accessing
    elements outside this range.
  • Programs should validate the correctness of all
    input values to prevent erroneous information
    from affecting a programs calculations.

22
Outline
  • fig06_08.c
  • (1 of 2 )

23
Outline
  • fig06_08.c
  • (2 of 2 )

24
Appendix C Keywords
  • auto break case char
  • const continue default do
  • double else enum extern
  • float for goto if
  • int long register return
  • short signed sizeof static
  • struct switch typedef union
  • unsigned void volatile while
Write a Comment
User Comments (0)
About PowerShow.com