SLIDE 1
1
CSCI 125 & 161 / ENGR 144 Lecture 7
Martin van Bommel
Control Statements
- Statements that affect the sequence of
execution of other statements
- Normal is sequential
- May want to repeat statements - iteration
- May or may not want particular statement
executed at all - conditional
Control Statement Structure
- Control line - first line (e.g. while)
- Body - statements enclosed in { } that are
repeated
- Body typically indented to show range of
control statement
Iteration and Loops
- Iteration - process of repeating an operation
- Essential for large amounts of data -
repeating statements shortens programs
- Loop - any portion of program repeated via
control statement
- e.g. while loop
The while statement
while (condition) { statements } where condition tested before first and every iteration statements executed each time condition true
while operation
- Condition test performed before every cycle,
including first.
- If condition is initially false,
body of loop not executed at all.
- Condition test performed only at beginning of
each loop cycle.
- Even if condition becomes false during cycle,