# ch06 - Chapter 6 Loops Chapter 6 Loops 1 Copyright 2008 W W...

This preview shows pages 1–8. Sign up to view the full content.

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

View Full Document
Chapter 6: Loops 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. All rights reserved. 2
Chapter 6: Loops 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. All rights reserved. 3

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

View Full Document
Chapter 6: Loops 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. All rights reserved. 4
Chapter 6: Loops 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. All rights reserved. 5

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

View Full Document
Chapter 6: Loops 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. All rights reserved. 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; } All rights reserved.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 62

ch06 - Chapter 6 Loops Chapter 6 Loops 1 Copyright 2008 W W...

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

View Full Document
Ask a homework question - tutors are online