Upload
dina-richardson
View
220
Download
0
Embed Size (px)
Citation preview
Iteration Statements
CGS 3460, Lecture 17Feb 17, 2006Hen-I Yang
Previously… while statement do … while statement
Agenda
for statement Throw in the towel
For Statements
for (expr1; expr2; expr3) statement;
it’s the best when there’s a counting variable Gives programmer better control over the loop expr1: initialization expr2: condition (control loop termination) expr3: updates performed at the end of each
iteration
For Statements (II)
for (expr1; expr2; expr3) statement;
==
expr1;
while (expr2) {
statement
expr3;
}
For Statements (III) Omitting some expressions in for loop Infinite loop using for Comma Operator
Used only in for or macro Can be used in expr1 or expr3 Always have a side effect Lowest Precedence
Glue expressions to where there should only be one single expression
Not often used
Throw in the Towel
Break; Find it! Abort search User input causes stop Good for exiting in the middle Work on the innermost statement, only exits one level
Continue; Not interested. Skip the rest of the loop, and go directly to the next Remain inside the loop Continue can only be used for loop, not switch e.g. add 0 (from user’s input)
Throw in the Towel (II)
Goto: BAD BAD BAD idea
goto label;label: statement Bad habit from Basic Unstructured. Both break and continue are restricted, but not got
o Use goto to emulate break; Use goto for multi-level break? Spaghetti code (Hard to read, hard to modify)
return; exit();
to be discussed later
Null statement
; Empty loop body At the end of compound statement A line by itself to avoid confusion Putting ; after () in if, while or for will ends t
he statemen
Sample Code
. .. …. .. … .
Miscellaneous/Update
Homework 2 ClarificationEPA and fuel efficiency calculationHow to print strings/tags/message properly
using conversion specifications? Quiz 1 will be returned early next week
Average: around 73 Homework 1 hopefully will be returned late
next week
Summary
For statement Throw in the towel
Before you go
Read Chapter 6. Homework 2 due date countdown: D - 5