lecture9-loops+methods

Lecture9-loops metho - Lecture 9 Loops Methods Lecture 9 Loops Methods CS 170 Section 000 Fall 2009 24 September 2009 September 2009 CS170 Section

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

View Full Document Right Arrow Icon
cture : Loops + Methods Lecture 9: Loops + Methods CS 170, Section 000, Fall 2009 4 ptember 2009 24 September 2009 9/24/2009 CS170, Section 000, Fall 2009 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture Plan Logistics Lab 4 questions? Loops Recap from last lecture break/continue more examples/case studies ¾ Methods defining (declaring) calling (invoking) parameters (arguments) 9/24/2009 CS170, Section 000, Fall 2009 2
Background image of page 2
Logistics Midterm 1: Postponed to Tuesday, October 6 th Will include material in Chapters 1 through 5 Optional Review/Seminar (Q/A) session: Monday afternoon, October 5 th omework 1 solutions to be posted by end of week Homework 1 solutions to be posted by end of week (not everyone has submitted yet) Homework 2 to be assigned today, due Wed 9/30 Solutions to be posted by end of week (in time for review session) 9/24/2009 CS170, Section 000, Fall 2009 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lab 4 questions? 9/24/2009 CS170, Section 000, Fall 2009 4
Background image of page 4
Loops Recap: while loop while (loop-continuation-condition) { int count = 0; // loop-body; Statement(s); while (count < 2) { System.out.println("Welcome to Java!"); count++; unt = 0; } } Loop Continuation ondition? false (count < 100)? false count = 0; Condition? true Statement(s) (loop body) true System.out.println("Welcome to Java!"); count++; 9/24/2009 CS170, Section 000, Fall 2009 5 9/22/2009
Background image of page 5

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

View Full DocumentRight Arrow Icon
SentinelValue.java : Compute Sum System.out.print("Enter int value (0 to exit): "); int data = input.nextInt(); nt um = 0; int sum = 0; while (data != 0) {// Keep reading until input=0 sum += data; System.out.print("Enter value (0 to exit):"); data = input.nextInt(); } System.out.println("The sum is " + sum); 9/24/2009 CS170, Section 000, Fall 2009 ~cs170000/inclass/Sept22/SentinelValue.java 6
Background image of page 6
Loops Recap: do-while Loop Statement(s) (loop body) Loop Continuation true do { // Loop body; Statement(s); Condition? false (); } while (loop-continuation-condition); 9/24/2009 CS170, Section 000, Fall 2009 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Recap: for Loops for (initial-action; loop- continuation-condition; for (int i=0; i<2; i++) { ystem out println( action-after-each-iteration) { // loop body; Statement(s); System.out.println( "Welcome to Java!"); } Initial-Action i = 0 } Loop Continuation Condition? false (i < 100)? false true Statement(s) (loop body) Action-After-Each-Iteration true System.out.println( "Welcome to Java"); i++ 9/24/2009 CS170, Section 000, Fall 2009 8
Background image of page 8
Recap: Which Loop to Use? • while and for loops are pre-test loops, do-while are post-test loops he three loops are expressively equivalent • The three loops are expressively equivalent. hile op in (a) below can always be converted into the r op in (b): A while loop in (a) below can always be converted into the for loop in (b): while (loop-continuation-condition) { // Loop body } Equivalent for ( ; loop-continuation-condition; ) // Loop body } A for loop in (a) below can generally be converted into the while loop in (b) except in certain special cases (see Review Question 3.19 for one of them): ( a ) ( b ) for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; Equivalent initial-action; while (loop-continuation-condition) { // Loop body; action-after-each-iteration; / /py ; } ( a ) ( b ) ; } 9/24/2009 CS170, Section 000, Fall 2009 9
Background image of page 9

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

View Full DocumentRight Arrow Icon
Recap: Nested Loops Print Triangle uter op for triangle rows Outer loop for triangle rows Inner loop for triangle columns for each row
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/03/2010 for the course CS CS 170 taught by Professor Eugeneagichtein during the Fall '09 term at Emory.

Page1 / 65

Lecture9-loops metho - Lecture 9 Loops Methods Lecture 9 Loops Methods CS 170 Section 000 Fall 2009 24 September 2009 September 2009 CS170 Section

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online