iteration statements cgs 3460, lecture 17 feb 17, 2006 hen-i yang
TRANSCRIPT
![Page 1: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/1.jpg)
Iteration Statements
CGS 3460, Lecture 17Feb 17, 2006Hen-I Yang
![Page 2: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/2.jpg)
Previously… while statement do … while statement
![Page 3: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/3.jpg)
Agenda
for statement Throw in the towel
![Page 4: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/9.jpg)
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 10: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/10.jpg)
Sample Code
. .. …. .. … .
![Page 11: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/12.jpg)
Summary
For statement Throw in the towel
![Page 13: Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang](https://reader036.vdocuments.net/reader036/viewer/2022082414/56649f385503460f94c54151/html5/thumbnails/13.jpg)
Before you go
Read Chapter 6. Homework 2 due date countdown: D - 5