{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 16-While Loop

Lecture 16-While Loop - CS1110 Oct 2010 Reading for next...

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

1 1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch. 8.1-8.3 (arrays) Prelim 2 : Tu Nov 9 th , 7:30-9pm. Last name A- Lewis : Olin 155 Last name Li -Z: Olin 255 Conflicts? Submit CMS "assignment" "P2 conflicts" by today . Review session : Sun Nov 7 th , 1-3pm, Phillips 101. ( Set your clocks back the night before!) Reminder: A5 due Sat. Oct 30th. See assignments pg for hints on snowflake geometry. A mystery (due to von Neumann): suppose you have a biased coin with unknown probability of heads p, 0 < p < 1. How can you use this coin to simulate the output of a fair coin? (Answer: while-loops …) 2 Beyond ranges of integers: the while loop while ( < condition > ) { sequence of statements } boolean expression. = "there's still stuff to do" the < repetend > In comparison to for-loops : we get a broader notion of “there’s still stuff to do” (not tied to integer ranges). But we must ensure that “condition” stops holding, since there’s no explicit increment. condition repetend false true 3 Canonical while loops // Process b..c for ( int k= b ; k <= c ; k= k+1 ) { Process k; } // Process b..c int k= b ; while ( k <= c ) { Process k; k= k+1; } Scope note : since k happened to be declared "within" the loop, k can’t be used after the loop. /* process a sequence of inputs where you don't know how many need to be taken care of */ <initialization> ; while ( <still input to deal with> ) { Process next piece of input; make ready for the next piece of input; } Here’s one way to use the while loop 4

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 ]}