# Register now to access 7 million high quality study materials (What's Course Hero?) Course Hero is the premier provider of high quality online educational resources. With millions of study documents, online tutors, digital flashcards and free courseware, Course Hero is helping students learn more efficiently and effectively. Whether you're interested in exploring new subjects or mastering key topics for your next exam, Course Hero has the tools you need to achieve your goals.

7 Pages

### lecture5

Course: CS 180, Fall 2009
School: Bentley
Rating:

Word Count: 770

#### Document Preview

5 cs180 Handout - Programming Fundamentals Fall'09 Page 1 of 7 Handout 5 Loops. Loops implement repetitive computation, a k a iteration. Java loop statements: while do-while for 1. Start with the while-loop. Syntax while(condition) Statement; //body of loop or while(condition) { //body of loop First_Statement; ... Last_Statement; } condition true Body of loop false Several logical loop organizations 1....

Register Now

#### Unformatted Document Excerpt

Coursehero >> Massachusetts >> Bentley >> CS 180

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
5 cs180 Handout - Programming Fundamentals Fall'09 Page 1 of 7 Handout 5 Loops. Loops implement repetitive computation, a k a iteration. Java loop statements: while do-while for 1. Start with the while-loop. Syntax while(condition) Statement; //body of loop or while(condition) { //body of loop First_Statement; ... Last_Statement; } condition true Body of loop false Several logical loop organizations 1. counting loops know exactly how many times to repeat a set of actions usually done with a use of a counter variable counter is initialized before the loop starts executing counter is updated after each iteration of the loop Example: print numbers from 0 to m. public class SimpleCountingLoop { public static void main(String[] args) { int i = 0; // the counter variable - to keep track of // number of times gone through the loop int m = 5; while (i < m){ System.out.println("i is " + i + " now."); i++; } System.out.println("Done. i is " + i ); } } Question: What will happen if m = -5? If the i++; in the loop body is omitted? Programming and Debugging Pitfalls: infinite loops , off by one -1- Handout 5 cs180 - Programming Fundamentals Fall'09 Page 2 of 7 Practice problems: for all the problems, assume Scanner kb = new Scanner (System.in); 1. Write a code segment that allows the user to enter 10 numbers and computes and prints out their sum, average, product. 2. Write a code segment that asks the user how many numbers there are in a list, then allows the user to enter the numbers and computes and prints out the smallest and the largest number. 3. Write a code segment that prints out all characters of a string one character per line. -2- Handout 5 cs180 - Programming Fundamentals Fall'09 Page 3 of 7 2.condition controlled loops repeat while a certain condition is true o sentinel-controlled loops stop repeating when a certain sentinel value is encountered Examples: 1. Let user input numbers until user enters 0. 0 is the sentinel in this case. Scanner kbrd = new Scanner (System.in); aNum = kbrd.nextInt(); while ( aNum != 0 ) { System.out.print(aNum); aNum = kbrd.nextInt() ; } Practice problems: 1. Write a code segment that continues to read a number from its user, until an even number is entered. 2. Write a code segment that continues to read user input until the user enters a number between 0 and 9. -3- Handout 5 cs180 - Programming Fundamentals Fall'09 Page 4 of 7 o boolean flag controlled loops stop when a boolean variable that reflects a certain state is false Example: describe what happens, when the following code segment executed. int is prevPos = 0; int currPos = 1; Scanner kbrd = new Scanner (System.in); String str = kbrd.nextLine(); boolean foundDouble = false; // boolean flag int len = str.length(); while (!foundDouble && currPos <= len - 1) { if (str.charAt(prevPos) == str.charAt(currPos)) foundDouble = true; else{ prevPos++; currPos++; } } -4- Handout 5 cs180 - Programming Fundamentals Fall'09 Page 5 of 7 2. do-while loop: similar to while-loop, but condition is checked at the end of each iteration. The body of the loop is going to be executed at least once. do Statement; while(condition); Body of loop //body of loop do { //body of loop First_Statement; ... Last_Statement; } while(condition); true condition false Practice problem: 1. Write a code segment that continues to read a number from the user, until the user enters an even number. Use a do-while loop. -5- Handout 5 cs180 - Programming Fundamentals Fall'09 Page 6 of 7 3. for loops especially useful for counting loops. Syntax: for (init-statements; loop-condition; update-statements) { //body of loop First_Statement; init-statements ... Last_Statement; } Loopcondition true Body of loop false Example: count down from 9 to 0 for( count = 9; count >= 0; count--) { System.out.print("T = " + count); System.out.println(" and counting"); } System.out.println("Blast off!"); Update-statements Practice problems. 1. Write a code segment that prints out a reverse of the string entered by the user, e.g. given "Walter" it would print "r...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Bentley - CS - 180
1Assignment #6CS 180 Programming Fundamentals Spring09Page 1 of 3Assignment 6: Reading and Programming Projects due 3/6. Reading AssignmentRead Section 3.3 Loops of the textbook completing self-test exercises. Review in-class practice prob
Minnesota - MATH - 1271
T UTORIAL S ERVICES - S PRING S EMESTER 2007Everyone Welcome - No Charge - For Information Call 612-624-2890Assistance in Mathematics, Physics, Chemistry, and other related courses150 LIND HALL - WALK-IN SERVICETUESD AY Shah MechEng/Sr Tim Aero/
Minnesota - ME - 3331
Chemical Thermodynamics 5Equilibrium in a System with Chemical Reaction Extent of ReactionHeat, Work and Energy. A First Course in Thermodynamics 2009, F. A. Kulacki Module 60 Slide &lt;#&gt; Chemical Thermodynamics-5Overview Review Reactions at Eq
Washington - CM - 420
CM 420 CM 420Temporary StructuresExcavations and Excavation SupportsCM 420 CM 420Temporary StructuresExcavations and Excavation SupportsCM 420Temporary StructuresCM 420 CM 420Temporary StructuresExcavation and Excavation SupportsIn
Washington - AEM - 547
Controllability and ObservabilityBefore we attempt to control a system to meet design specications, we must know whether such a controller exists. Specically, given a system of the form x(t) = A(t)x(t) + B(t)u(t) y(t) = C(t)x(t) + D(t)u(t) we would
Washington - SIS - 200
Crusades Seven Crusades between the 11th and 13th Centuries The capture of Jerusalem (1095-1291) The Latin Kingdom of Constantinople (1204-1261)CrusadesJews Executed During CrusadesConsequences Transportation Financing New commodities
Washington - BIOL - 481
Oregon State - ECE - 112
Section 3Motors and BJT'sSection 3: Motors and BJT'sSection OverviewSemiconductors are amazing. This section covers the functionality of two common types of semiconductors, diodes and Bipolar Junction Transistors (BJTs). Diodes allow current to
Oregon State - ECE - 473
Copyright 2003 Oregon State University1 of 2Lab 6 Using the Serial Peripheral Interface (SPI)In this lab a digitally adjustable potentiometer will be added to control the level (volume) of the audio buffer amplifier, from lab three, as well as
Washington - CHEM - 364
CHEM 364, Lecture #111. Powder X-ray Diffraction (PXRD): http:/www.matter.org.uk/diffraction/x-ray/powder_method.htm Powder X-ray diffraction (PXRD) is perhaps the most widely used technique for characterizing solid materials. As the name suggests,
Minnesota - KAYNI - 002
Curriculum Vitae for Mariam KayniaContact InformationName: Mariam Kaynia Address: 220 Delaware St. S.E. #632 Minneapolis, MN 55455 Phone: +1-612-735-1565 / +47-99352925 E-mail: kayni002@umn.edu / kaynia@stud.ntnu.noEducationField of Study: Elect
Minnesota - SCHN - 0462
Please wait for a site operator to respond.You are currently number 2 of 2 in the queue. Thank you for your patience.Chat InformationYou are now chatting with 'Greg C'Greg C: Hello, Rob. Welcome to j2 Global online live support. I am Greg, your Onl
Penn State - CHEM - 431
The Synthesis of NMP, a Fluoxetine (Prozac) Precursor, in the Introductory Organic LaboratoryRevised By: Chad FriedmanWEEK ONE Synthesis of 2: (+ or -)-3-(dimethylamino)-1-phenylpropanol. Weigh 2.00g of 3-dimethylaminopropiophenone hydrochloride (
Oregon State - CS - 570
Project Proposal - Architectural Support for Multithreading Introduction: The origin of Multithreaded systems goes right back to 1950's. Yet, the first commercial multithreaded system, Heterogeneous Element Processor (HEP), was introduced in 1978 onl
Penn State - STAT - 597
Penn State - SECTION - 415
TreatObserv157157151515111592012201720122018201825142518251825192519301930253022301930233573510351135153511
Penn State - STAT - 418
Exam 2 SolutionsSTAT/MATH 418Fall 20071. The probability of being dealt a full house in a hand of poker is approximately 0.0014. Find an approximation for the probability that in 800 hands of poker you will be dealt at least one full house. Wha
Penn State - SECTION - 415
TreatObserv11.0311.0411.0711.0311.0811.0611.0721.0321.1021.0821.0521.0621.0621.0531.0331.0831.0631.0231.0431.0431.0741.1041.1041.0941.0941.0641.0541.0851.0451.0651.0751.0651.0551.07
Penn State - SECTION - 415
TreatObserv15191618211213210212310363939
Penn State - SECTION - 415
W6.97.36.76.46.35.97.07.16.57.67.27.16.17.37.67.66.76.35.76.77.55.35.47.46.9
Penn State - SECTION - 415
Barium12222333333333333344444444444444444445555555555555555666666666666666777777777888888888888999999910101112
Penn State - SECTION - 415
Penn State - SECTION - 415
x0000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111
Penn State - SECTION - 415
TempDRings53 357 158 163 170 170 175 2
Penn State - SECTION - 415
RowColumnX111612181321142121142215231824173115321533183416
Penn State - SECTION - 415
YesNo1281327
Penn State - SECTION - 415
Oregon State - CS - 325
Due: Friday 13 MarCS325: Homework #7 Rudrata's Problem or the Knight's Tour1A knight is a chesspiece which can legally move from a square (i, j) on a chessboard (where i is the row index, and j is the column index) to any of the eight squares (i -
Oregon State - GEO - 510
VR Tours Presentation Update6/24/08Purpose We seek collaboration for a campus VR tour project that will assist web-users in understanding, navigating, and exploring the university campus. The intent is create an end product that enhances current
Minnesota - MATH - 2374
1. Let f (x, y, z) = xy 3 . Evaluate the line integral c f (x, y)ds over the curve C(t) = (4 sin t, 4 cos t, 3t) where 0 t /2. (If you are confused how to start, here are some steps to try) What are the start and end points of the path C(t)? Writ
Minnesota - MATH - 2374
1. Evaluate the integral 1 1-y 2x2 +y 2xyzdzdxdy0 0 x2 +y 2Sketch the region of integration.What coordinate change should we use? Setup the integral.What is the answer?2. Find the volume of the region between a sphere with radius 1 an
Minnesota - MATH - 2374
For the next several problems we will be working with the function f (x, y) = xy 3 1. Let f (x, y, z) = xy 3 . Evaluate the line integral c f (x, y)ds over the curve C(t) = (4 sin t, 4 cos t, 3t) where 0 t /2. (If you are confused how to start, her
University of Texas - SUDMUKD - 95920
Copyright by Cholthicha Sudmuk 2005The Dissertation Committee for Cholthicha Sudmuk Certifies that this is the approved version of the following dissertation:THE SYNTAX AND SEMANTICS OF SERIAL VERB CONSTRUCTIONS IN THAICommittee: Stephen Wechsl
Penn State - IST - 421
Business Process Overview5. Business Process: OverviewR SAP AGUnit Purpose The purpose of this unit is to expand on the introduction of the SAP R/3 System by explaining how it supports business processes.?SAP AGSAP20 SAP R/3 Overview (Inte
Minnesota - MATH - 4707
Math 4707Introduction to Combinatorics and Graph Theory Homework 4, Due Monday October 23Fall 2006Exercises in square brackets [ ] are to be worked out but not handed in. All answers must be supported. If a logical argument is required, use ful
Minnesota - SENG - 5831
Classification of WCET* Analysis Techniques R. Kirner and P. Puschner Int'l Symposium on Object-Oriented Real-Time Distributed Computing, 2005 1) WCET calculation transforms to the Halting Problem (loosely interpreted: you can't know the full path of
Penn State - CHEM - 408
Penn State - PDT - 113
PATRICKD.THORNTONE-mail: pdt113@psu.edu_EDUCATION:2005-Present THE PENN STATE UNIVERSITY, University Park, PA B.S. in the Smeal College of Business, May 2009 3.17 G.P.A. Recipient of the Neponset Valley Leadership Institute, Norwood Scholars
Penn State - EJB - 5048
Edward BeaurainSchool Address: 125 East Fairmount, Appt. B State College, PA 16801 (856) 465-8670 Ejb5048@psu.eduEDUCATION:Permanent Address: 19 Brianna Road Holland, PA 18966 (215) 860-5425 Ejb5048@psu.eduThe Pennsylvania State University Bach
Minnesota - ME - 3331
Chemical Thermodynamics - 2Mixtures of Ideal Gases The Chemical Potential Chemical EquilibriumHeat, Work and Energy. A First Course in Thermodynamics 2009, F. A. Kulacki Module 57 Slide # Chemical Thermodynamics-2Overview Review Gibbs functio
Minnesota - CSCI - 8701
Euclidean Space BYLINE [Name of expert writing the entry, expert's affiliation] SYNONYMS Euclidean Space, Cartesian Space DEFINITION Euclidean space is an important part of Euclidean geometry, which was developed by Euclid, a great Greek mathematicia
Penn State - PHYS - 151
Physics 151/251 Ohms Law Lab Group members:Purpose To determine the variation of Voltage with current for conductors and compare that to nonconductors. Apparatus DC generator, ammeter, digital volt meter, thin wire, thick wire, plastic wire and a sw
Long Island U. - LIS - 510
LIS510 lecture 11 Thomas Krichel 2005-03-23today we do the history of American libraries only. This is from page 273 to 298 in the book. I also consulted Fourie and Dowell, &quot;Libraries in the Information Age&quot;. Structure is foreword American li
Long Island U. - LIS - 9013
LIS901-03User interfaces 20090326See the course web site at http:/openlib.org/home/krichel/courses/lis9013n09a for the latest online version of this file. Course Description This course extends LIS650 to build interactive web user interfaces. While
Penn State - MATH - 231
MATH 231 MIDTERM EXAM October 25, 2007NAME STUDENT NUMBER INSTRUCTOR SECTION NUMBER There are 10 problems in this exam (6 multiple choice and 4 partial credit problems). Circle exactly one answer for the multiple choice problems. Present your w
Long Island U. - ECO - 9103
Eco 9103 Econometrics I - Final Exam, Fall 2008 1. Suppose thatDr. Tavis Barr' y=X is a true econometric model, where X X /n a.c. Q1 , ' 2 ' X X /n a.c. Q 2 , Q being a positive definite matrix, and X / n a.c. N 0, Q2 , where Q1, Q2, an
Long Island U. - ECO - 9103
Eco 9103 Econometrics I - Final Exam, Fall 2008 1. Suppose thatDr. Tavis Barr' y=X is a true econometric model, where X X /n a.c. Q1 , ' 2 ' X X /n a.c. Q 2 , Q being a positive definite matrix, and X / n a.c. N 0, Q2 , where Q1, Q2, a
Minnesota - CSCI - 1001
CHAPTER 6 NOTES: A CASE STUDY IN ONLINE RESEARCH=Page 147: When doing research it is important to remember that manybooks, journals, etc. currently exist only in hardcopy and are notonline. Therefore thoroughly researching a topic involves both
University of Texas - CS - 372
Virtual Memory and Address TranslationDepartment of Computer Sciences The University of Texas at AustinHarrick Vinvin@cs.utexas.edu1Virtual MemoryConceptKey problem: How can one support programs that require more memory than is physicall
Penn State - JAK - 535
The Rainbow Connection: LGBTQ StudentsApril 30th, 2008Chiquita Baylor Ivan Ceballos Stephanie Chang Jamie Kammerman-Watson Bryan KovalOverview Key Terms Demographics Diagram of Sex and Gender Relevant Theories Current Research Interviews O
Penn State - DCB - 217
DEREK C. BAUER CONSTRUCTION MANAGEMENT DR. MICHAEL HORMANTECHNICAL ASSIGNMENT #2OFFICE/RETAIL BUILDING WASHINGTON, D.C.SUBMITTED:NOVEMBER 2, 2007TABLE OF CONTENTSExecutive Summary .. 2 Detailed Project Schedule . 3 Site Layout Planning -
Penn State - KML - 5016
218 S. SPARKS ST. APT 304 STATE COLLEGE, PA 16801 CELL: 570-582-7449 EMAIL: KML5016@PSU.EDU WWW.ENGR.PSU.EDU/AE/THESIS/PORTFOLIOS/2009/KML5016Kristen M. LechnerOBJECTIVESeeking a summer internship in the structural field of Architectural Engin
Penn State - MPG - 5001
MichaelP.Gardner Lighting/Electrical Advisors:Mistrick/Dannerth MixedUseProject MetropolitanEasternUnitedStates 16December2008PROPOSAL EXECUTIVESUMMARY Theproposedthesiswillincludenewlightingdesignsfortheresidentiallobby,fitness room,retai
Penn State - DFH - 124
ContentsExecutiveSummary.2 DesignAnalysis ..3 . EnergyAnalysis.6 References.7 AppendixAWeatherDataforWilkesBarrePennsylvania..8 AppendixBOccupancySchedule(DesignDay).9 AppendixCLightingSchedule(DesignDay)..10 AppendixDMiscellaneousEquipmentSchedu
Penn State - CSE - 597
Garnet Application ModelBo Zhao CSE597A The Pennsylvania State University Application Framework Application Structure and Lifecycle API Survey Development Environment Sample Application2OS Architecture Garnet OS API: Hardware independe
Penn State - CSE - 597
Symbian OS, Part 2: Application ModelCSE 597A Cellular Phone OS Security Wenhui Hu1NewsNokia's Ovi (portal) StoreMay 2009Symbian FoundationAvailable to foundation members, 1H 2009 First unified foundation release, expected in 2009 Open so
Duquesne - LEGISLATIV - 1968
Duquesne - A - 1984
SENATE AMENDED PRIOR PRINTER'S NO. 203 PRINTER'S NO.3375THE GENERAL ASSEMBLY OF PENNSYLVANIAHOUSE BILL No. 202A JOINT RESOLUTION 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19Session of 1981INTRODUCED BY WILSON AND KOLTER, JANUARY 26, 198
Duquesne - A - 1985
PRIOR PRINTER'S NO. 1026PRINTER'S NO.1964THE GENERAL ASSEMBLY OF PENNSYLVANIAHOUSE BILL No. 902Session of 1983INTRODUCED BY ITKIN, McVERRY, DAWIDA, COWELL, PISTELLA, VAN HORNE, MARKOSEK, TRELLO, OLASZ, DeLUCA AND PRESTON, MAY 2, 1983 AS R
Duquesne - A - 1973
776 No.8LAWS OF PENNSYLVANIA,A JOINT RESOLUTION HB 582 Proposing an amendment to article eight, section two of the Constitution of the Commonwealth of Pennsylvania further providing for the establishment of standards and qualifications for land u
Minnesota - ECON - 8207
1Outline.1. Intro and Motivation.2. Modeling Approaches in Microeconometrics3. Data Structures Welcome to 8206. This is 3rd mini of grad econometrics sequence. Focus will be on microeconometrics (BTW, next year time series will be offer i