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

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. | Iterative Statements: while, for, do-while while (chapter 8- Java also has a “for each” loop for traversing lists and other structures. It is explained later, in Chapters 12 and 20. Objectives: Understand the semantics and learn the Java syntax for while, for, and do-while loops Learn how to use nested loops 8- Also learn about perfect numbers. 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 (. loops): for, while, and do-while. 8- Iterations are what makes a program different from a cookbook recipe. The . | Iterative Statements: while, for, do-while while (chapter 8- Java also has a “for each” loop for traversing lists and other structures. It is explained later, in Chapters 12 and 20. Objectives: Understand the semantics and learn the Java syntax for while, for, and do-while loops Learn how to use nested loops 8- Also learn about perfect numbers. 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 (. loops): for, while, and do-while. 8- Iterations are what makes a program different from a cookbook recipe. The while Loop while ( condition ) { statement1; statement2; . statementN; } while ( condition ) statement1; If the body has only one statement, the braces are optional condition is any logical expression, as in if The body of the loop 8- Many people prefer to always use braces, as it is easier to add statements to the body of the loop. // Returns the smallest n // such that 2^n >= x public static int intLog2 (int x) { int n = 0, p = 1; while ( p 8- “Change” can be increment, decrement, or any other operation. 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 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
7    65    1    15-05-2024
77    355    9    15-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.