1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 8 - Maria Litvin, Gary Litvin

29 26 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 29
Dung lượng 323,14 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 8 - Iterative statements: while, for, do–while. In this chapter, the learning objectives are: Understand the semantics and learn the Java syntax for while, for, and do-while loops; learn how to use nested loops.

Trang 1

Iterative Statements: while, for,

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 3

Iterations

• It is essential that a program be able to

execute the same set of instructions many

times: otherwise a computer would do only as much work as a programmer!

• Repeating the same code fragment several

times is called iterating.

• Java provides three control statements for

iterations (a.k.a loops): for, while, and

do-while

Trang 4

If the body has only

one statement, the

braces are optional

condition is any

logical expression,

as in if

The body of the loop

Trang 6

The while Loop (cont’d)

• Initialization: The variables tested in the

condition must be initialized to some values

If the condition is false at the outset, the loop

is never entered

• Testing: The condition is tested before each iteration If false, the program continues with the first statement after the loop

• Change: At least one of the variables tested

in the condition must change within the body

of the loop

Trang 7

The while Loop (cont’d)

• Sometimes change is implicit in the changed state of a variable:

Scanner input = new Scanner(inputFile);

while (input.hasNext())

System.out.println ( input.next( ) );

Changes the

state of input

Trang 9

• for is a shorthand that combines in one statement initialization, condition, and change:

for ( initialization; condition; change )

Trang 11

• Java allows you to declare the loop control variable in the for statement itself For

The scope of k is the

body of the loop, and

k is undefined outside

the loop

Trang 12

“Repeat n times” idiom:

Trang 14

Always use braces

for readability (even

if the body has only

one statement)

if condition is false,

the next iteration is not executed

Trang 15

The do-while Loop (cont’d)

• do-while is convenient when the variables

tested in the condition are calculated or read

in the body of the loop:

Trang 16

The do-while Loop (cont’d)

• do-while can be easily avoided: we can

usually replace it with a while loop initialized

so that it goes through the first iteration:

Trang 17

break and return in Loops

• break in a loop instructs the program to

immediately quit the current iteration and go

to the first statement following the loop

• return in a loop instructs the program to

immediately quit the current method and

return to the calling method

• A break or return must be inside an if or an

else, otherwise the code after it in the body of the loop will be unreachable

Trang 19

return in Loops

• Sequential Search method:

public int search(String[ ] list, String word)

Trang 21

Nested Loops (cont’d)

• Braces are optional when the body of the

loop(s) is one statement:

for’s body

• Many programmers prefer to always use

braces in loops, especially in nested loops

Trang 22

Nested Loops (cont’d)

• Be careful with break:

for (int r = 0; r < m.length; r++)

continues with the outer loop

Trang 23

“Triangular” Nested Loops

• “Find duplicates” idiom:

for (int i = 0; i < a.length; i++)

}

The inner lcv starts at the outer lcv’s next value

Trang 24

Lab: Perfect Numbers

• A perfect number is a positive integer equal to the sum of all its divisors (including 1 but

excluding the number itself) For example:

28 = 1 + 2 + 4 + 7 + 14

Write a program to find the first four perfect

numbers (Java Methods pp 210-211).

Trang 25

Lab: Perfect Numbers (cont’d)

• Euclid showed that if 2n-1 is

Trang 26

Lab: Perfect Numbers (cont’d)

• A prime that has a form

Write a program to find the first six Mersenne

primes and the corresponding perfect numbers.

• Euler proved that any even perfect number must have that form

Trang 27

Lab: Perfect Numbers (cont’d)

• The largest known Mersenne Prime is

243,112,609 – 1 It has 12,978,189 digits It

was discovered by GIMPS (Great Internet

Mersenne Prime Search) particiants at

UCLA in 2008

• They also claimed the Electronic Frontier

Foundation’s $100,000 prize for finding a

prime number with more than 10 million

digits

http://www.mersenne.org

Trang 28

• Can any code with a for loop be

rewritten with a while loop?

• Does short-circuit evaluation apply to conditions in loops?

• Which operators can be used in the

“change” part of a for loop?

Trang 29

Review (cont’d):

• Are method calls allowed in a condition

in a while loop?

• Is return allowed in a loop?

• What is a nested loop?

• Name a situation where nested loops are used

• Can you have nested while loops?

Ngày đăng: 04/11/2020, 23:15

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN