13
Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Embed Size (px)

Citation preview

Page 1: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Iteration Statements

CGS 3460, Lecture 17Feb 17, 2006Hen-I Yang

Page 2: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Previously… while statement do … while statement

Page 3: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Agenda

for statement Throw in the towel

Page 4: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

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

Page 5: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

For Statements (II)

for (expr1; expr2; expr3) statement;

==

expr1;

while (expr2) {

statement

expr3;

}

Page 6: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

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

Page 7: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

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)

Page 8: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

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

Page 9: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

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

Page 11: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

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

Page 12: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Summary

For statement Throw in the towel

Page 13: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang

Before you go

Read Chapter 6. Homework 2 due date countdown: D - 5