Loops and Loop Programming Idioms

Loops and Loop Programming Idioms - Loops in C Objectives...

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

View Full Document Right Arrow Icon
Loops in C
Background image of page 1

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

View Full DocumentRight Arrow Icon
Objectives Review syntax and semantics of loops Become familiar with some basic idioms
Background image of page 2
while Loop The only one you ever need while ( condition ) { statements } condition evaluated before each iteration If it evaluates to zero, the loop exits without doing the next iteration (even if zero iterations have been done) statements zero or more statements
Background image of page 3

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

View Full DocumentRight Arrow Icon
Do All of Em Idiom: display the digits 1 to 10 int k = 1; while (k < 11) { printf(“%d ”, k); k = k + 1; }
Background image of page 4
Search/Seek Idiom: is x a perfect square? int x; // initialized elsewhere int root = 1; while (root * root < x) { root = root + 1; } if (root * root == x) { printf(“%d is the square root of %d”, root, x); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
if with while if (x > y) { z = x; } else { z = y; } while (x > y && z != x) { z = x; } while (x <= y && z != y) { z = y; }
Background image of page 6
break Causes the loop to immediately exit int k = 0; while (1) { // always true
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background 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 07/24/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 10

Loops and Loop Programming Idioms - Loops in C Objectives...

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