Title: Break and continue statements Array Examples
1Lecture 10
- Break and continue statements Array Examples
- Reference Section 4.9, 4.12, 6.4
2break 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)
4break 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)
6Summary 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
7Summary of Structured Programming
8Summary of Structured Programming
9Rules 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
10Rules for Forming Structured Programs
Simplest flowchart
11Rules for Forming Structured Programs
Repeatedly applying rule 2 (stacking rule) to the
simplest flowchart
12Rules for Forming Structured Programs
Applying rule 3 (nesting rule) to the simplest
flowchart.
13Array 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
14Outline
15Outline
16Common 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.
17Outline
Element Value 0 2 1
4 2 6 3
8 4 10 5 12
6 14 7 16 8
18 9 20
18Outline
19Outline
20Outline
21Error-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.
22Outline
23Outline
24Appendix 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