1 / 8

Loop

Loop . 10/22/2012. Outline. Loop 1: for Loop 2: while Loop 3: do while Practice. f or loop (1/2). for (expression) { statement_1; statement_2 ; } If the statement body only has one statement, the left brace “{” and right brace “}” are often dropped for ( expression) statement.

jamar
Download Presentation

Loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loop 10/22/2012

  2. Outline • Loop 1: for • Loop 2: while • Loop 3: do while • Practice

  3. for loop (1/2) • for (expression) { statement_1; statement_2; } • If the statement body only has one statement, the left brace “{” and right brace “}” are often dropped • for (expression) statement Control variable Initialization Condition Increment Result: Example:

  4. for loop (2/2) • A possible mistake • The Increment is forgotten Example: Result:

  5. while loop (1/2) • while (expression) { statement_1; statement_2; } • If the statement body only has onestatement, the left brace “{” and right brace “}” are often dropped • while (expression) statement; Example: Result:

  6. while loop (2/2) • 2 possible mistake: • The left and right brace are forgotten • while (expression); Example: Result: Example: Result:

  7. do while loop • do { statement_1; statement_2; } while (expression); • If the statement body only has one statement, the left brace “{” and right brace “}” are often dropped • while (expression) statement; • The semicolon “;” at the end of while cannot be omitted Example: Result:

  8. Practice Time • Greatest Common Divisor (GCD) • GCD of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder • For example, the GCD of 8 and 12 is 4 • Sample input • Sample output • Hint • Euclidean algorithm Example: 2 12 1 8 4 8 0 4

More Related