chapter06 - COP 3275: Chapter 06 Jonathan C.L. Liu, Ph.D....

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3275: Chapter 06 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Iteration Statements C’s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body ). In C, every loop has a controlling expression . Each time the loop body is executed (an iteration of the loop), the controlling expression is evaluated. If the expression is true (has a value that’s not zero) the loop continues to execute. 2
Background image of page 2
Iteration Statements C provides three iteration statements: The while statement is used for loops whose controlling expression is tested before the loop body is executed. The do statement is used if the expression is tested after the loop body is executed. The for statement is convenient for loops that increment or decrement a counting variable. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The while Statement Using a while statement is the easiest way to set up a loop. The while statement has the form while ( expression ) statement expression is the controlling expression; statement is the loop body. 4
Background image of page 4
The while Statement Example of a while statement: while (i < n) /* controlling expression */ i = i * 2; /* loop body */ When a while statement is executed, the controlling expression is evaluated first. If its value is nonzero (true), the loop body is executed and the expression is tested again. The process continues until the controlling expression eventually has the value zero. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The while Statement A while statement that computes the smallest power of 2 that is greater than or equal to a number n : i = 1; while (i < n) i = i * 2; A trace of the loop when n has the value 10: i = 1; i is now 1. Is i < n ? Yes; continue. i = i * 2;i is now 2. Is i < n ? Yes; continue. i = i * 2;i is now 4. Is i < n ? Yes; continue. i = i * 2;i is now 8. Is i < n ? Yes; continue. i = i * 2;i is now 16. Is i < n ? No; exit from loop. 6
Background image of page 6
The while Statement Although the loop body must be a single statement, that’s merely a technicality. If multiple statements are needed, use braces to create a single compound statement: while (i > 0) { printf("T minus %d and counting\n", i); i--; } Some programmers always use braces, even when they’re not strictly necessary: while (i < n) { i = i * 2; } 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The while Statement The following statements display a series of “countdown” messages: i = 10; while (i > 0) { printf("T minus %d and counting\n", i); i--; } The final message printed is T minus 1 and counting . 8
Background image of page 8
The while Statement Observations about the while statement: The controlling expression is false when a while loop terminates. Thus, when a loop controlled by i > 0 terminates, i must be less than or equal to 0. The body of a while loop may not be executed at all, because the controlling expression is tested before the body is executed. A
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2011 for the course COP 3275 taught by Professor Jonathanliu during the Fall '11 term at University of Florida.

Page1 / 62

chapter06 - COP 3275: Chapter 06 Jonathan C.L. Liu, Ph.D....

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online