Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang."— Presentation transcript:

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

2 Previously… while statement do … while statement

3 Agenda for statement Throw in the towel

4 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

5 For Statements (II) for (expr1; expr2; expr3) statement; == expr1; while (expr2) { statement expr3; }

6 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

7 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)

8 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 goto  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

9 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 the statemen

10 Sample Code.... ….... …...….

11 Miscellaneous/Update Homework 2 Clarification  EPA and fuel efficiency calculation  How 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

12 Summary For statement Throw in the towel

13 Before you go Read Chapter 6. Homework 2 due date countdown: D - 5


Download ppt "Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang."

Similar presentations


Ads by Google