lecture7 - Administrative Issues Program Statements Part 3...

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

View Full Document Right Arrow Icon
Program Statements Part 3 Lecture 7 Administrative Issues • Nothing New • Nothing Due • Ongoing – Program 2 • Due Wednesday – Homework 1 • Due Tomorrow While Loop • Repeats a block of code until a condition is false while (<expr>) { ... } While Loop Example • Print out a line with many periods int count=0; while (count < 10) { System.out.print(“.”); count++; } • A loop has three major parts int count=0; while (count < 10) { System.out.print(“.”); count++; } Three Parts to a Loop Preconditions Conditional Update Two Minute Exercise • Write a code fragment that plays the “Hi/Low” game • Assume the following code has already been written: Random generator = new Random(); int answerValue = generator.nextInt(100)+1; • Ask the user to guess a number between 1 and 100 • If the guess is right, print “Congrats.” • If the guess was wrong, print “Too high.” or “Too low.”
Background image of page 1

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

View Full DocumentRight Arrow Icon
Nested Loops • Just like if statements, loops can be nested • What does this code fragment do? int lineNum=0, dots=0; while (lineNum < NUM_OF_LINES) { dots = 0; while (dots <= lineNum) { System.out.print(“.”); dots++; } System.out.println(); lineNum++; } Parsing String s • Parsing a string is a common use of loops • Example: Given a list of values String data=“John Senior Econ”; • How can we break up the string into pieces? String methods • Iterate and use charAt() to look for space •U s e substr() to copy data – Better way?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/15/2009 for the course ASTRONOMY 1000 taught by Professor Kim during the Spring '01 term at Yonsei University.

Page1 / 6

lecture7 - Administrative Issues Program Statements Part 3...

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

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