tailieunhanh - Lecture note Java methods A & AB: Object-oriented programming and data structures: Chapter 8 - Maria Litvin, Gary Litvin

Chapter 8 - Iterative statements: while, for, do-while. After you have mastered the material in this chapter, you will be able to: 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 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. while (chapter 8- Java also has a “for each” loop for traversing lists and other structures. It is explained later, in Chapters 12 and 19. 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 | Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. while (chapter 8- Java also has a “for each” loop for traversing lists and other structures. It is explained later, in Chapters 12 and 19. 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 change within the body of the