lec5 - Page 1 of 27 CSE 8A UCSD LEC 5 CSE 8A Lecture 5 ✔...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Page 1 of 27 CSE 8A, UCSD LEC 5 CSE 8A: Lecture 5 ✔ Iteration ✔ while and do-while loops ✔ Loop invariants ✔ Methods ✔ Procedural abstraction (Reading: Savitch, parts of Ch. 3 and 4) Page 2 of 27 CSE 8A, UCSD LEC 5 Iterative (looping) control statements in Java ✔ Iterative control statements, also known as ‘loops’, contain substatements which are executed iteratively (i.e., over and over) ✗ Computers are good at doing things repetitively, precisely, over and over ✗ Humans get bored doing that. So, get a machine to do it, and let people do something more interesting ✔ Iteration is another essential part of programming a computer to do something interesting ✗ (In fact, with iteration, a program can compute any function... Iteration is an extremely powerful computational trick, and so can be extremely tricky to get right) ✔ Iterative control constructs in Java: while statements do-while statements for statements (we will cover for statements later) Page 3 of 27 CSE 8A, UCSD LEC 5 The while statement ✔ Syntax: A while statement in Java has the form while ( boolean_expression ) statement ✔ Semantics: When a while statement is executed... ✗ The boolean_expression is evaluated. • If it is “true”, the loop body statement is executed, and then the while statement is executed again • If it is “false”, the loop body statement is not executed, and control passes beyond the while statement ✔ The loop body can be any kind of Java statement Page 4 of 27 CSE 8A, UCSD LEC 5 While-loop flowchart boolean_expression true false body next statement previous statement Page 5 of 27 CSE 8A, UCSD LEC 5 An advantage of iteration... ✔ The user will enter 100 integers at the terminal, one per line ✔ Compute and print the average of the numbers: Scanner in = new Scanner(System.in); int n, sum=0; System.out.print("Enter next number: "); n = in.nextInt(); sum += n; System.out.print("Enter next number: "); n = in.nextInt(); sum += n; ... // 100 of these! System.out.println("The average is " + sum / 100.0 ); ✔ This takes a lot of lines of code! You can write it much more compactly using an iterative control construct ✔ (Any time you find yourself writing repeated code segments like this, it is a sign you should think about writing a loop instead.) Page 6 of 27 CSE 8A, UCSD LEC 5 Example: averaging 100 integers ✔ Easy with iteration: Scanner in = new Scanner(System.in); int n, sum=0, i=0; while(i < 100) { System.out.print("Enter a number: "); n = in.nextInt(); sum += n; i++; } System.out.println("The average is " + sum / 100.0 ); Page 7 of 27 CSE 8A, UCSD LEC 5 The need for iteration... ✔ Compute the average of numbers entered from the terminal, one per line, up to the first negative number entered ✔ A negative number is a ‘sentinel value’... it is used just to signal the end of input ✔ So, you when writing the program, you don’t know how many numbers will be entered ✔ Can’t solve this correctly without iteration! Can’t solve this correctly without iteration!...
View Full Document

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 27

lec5 - Page 1 of 27 CSE 8A UCSD LEC 5 CSE 8A Lecture 5 ✔...

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

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