lecture04

lecture04 - CS111: Slides for Prof. Steinberg s lecture 1...

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

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 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 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.

Unformatted text preview: CS111: Slides for Prof. Steinberg s lecture 1 Lecture 04 Computer Science 111 Computer Science 111 Lecture 04 Java CS111: Slides for Prof. Steinberg s lecture 2 Lecture 04 Repeating things: while Repeating things: while New way of combining operations: while Combines: A condition One or more operations Test the condition If false, go to end of the while and continue from there Otherwise, do the operations & go back to start of the while CS111: Slides for Prof. Steinberg s lecture 3 Lecture 04 Repeating things: while Repeating things: while Test, do, , test, exit loop Eg. Copy up to but not including sentinel Input data data != sentinel false true Print data Input data CS111: Slides for Prof. Steinberg s lecture 4 Lecture 04 Repeating things: do-while Repeating things: do-while Do, test, , test, exit loop Eg. Copy up to and including sentinel Input data data != sentinel false true Print data CS111: Slides for Prof. Steinberg s lecture 5 Lecture 04 While While vs vs Do-While Do-While condition false true operation condition false true operation While Do - While CS111: Slides for Prof. Steinberg s lecture 6 Lecture 04 Summary Variables Summary Variables Sometimes we have a sequence of numbers and want to store one number at a time but still operate on all the numbers E.g., add them all Solution: do the operation step by step Eg to add 4, 3, 5, and 9 do: 0+4 = 4 4+3 = 7 7+5 = 12 12+9 = 21 CS111: Slides for Prof. Steinberg s lecture 7 Lecture 04 Summary Variables Summary Variables At any one point in time we only need One number from the sequence The running total-so-far I.e., only two variables, no matter how lomg a sequence A variable used to hold a running total-so-far is called a summary variable or an accumulator CS111: Slides for Prof. Steinberg s lecture 8 Lecture 04 Summary Variables Summary Variables Inside the loop, update the summary variable Before the loop, initialize the summary variable Often to 0 or 1 Sometimes to first number CS111: Slides for Prof. Steinberg s lecture 9 Lecture 04 Counted loops Counted loops An alternative to sentinel: say how many numbers are in the sequence Input length of sequence of numbers to read Count numbers as you read them Repeat as long as you have not read enough E.G. How many? 2 next number? 14 next number? 23 sum: 37 CS111: Slides for Prof. Steinberg s lecture 10 Lecture 04 Nested Loops Nested Loops What if the operations in a while contain another while? CS111: Slides for Prof. Steinberg s lecture 11 Lecture 04 Nested Loops Nested Loops col < 4 ?...
View Full Document

This document was uploaded on 11/01/2011 for the course 198 111 at Rutgers.

Page1 / 49

lecture04 - CS111: Slides for Prof. Steinberg s lecture 1...

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

View Full Document
Ask a homework question - tutors are online