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

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

View Full Document Right Arrow Icon
Chapter 6: Loops All rights reserved. 1 Chapter 6 Loops
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full Document Right Arrow Icon
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
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; } All rights reserved.
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online