Loops and Loop Programming Idioms

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

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

Loops in C

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

View Full Document
Objectives Review syntax and semantics of loops Become familiar with some basic idioms
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

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

View Full Document
Do All of Em Idiom: display the digits 1 to 10 int k = 1; while (k < 11) { printf(“%d ”, k); k = k + 1; }
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); }

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

View Full Document
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; }
break Causes the loop to immediately exit int k = 0; while (1) { // always true

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

View Full Document

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.

## 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
Ask a homework question - tutors are online