Java - profthinh ď jhtp5_05 tài liệu, giáo án, bài giảng , luận văn, luận án, đồ án, bài tập lớn về tất cả các lĩnh vực...
1 Chapter – Control Structures: Part Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Statement 5.4 Examples Using the for Statement 5.5 do…while Repetition Statement 5.6 switch Multiple-Selection Statement 5.7 break and continue Statements 5.8 Labeled break and continue Statements 5.9 Logical Operators 5.10 Structured Programming Summary 5.11 (Optional Case Study) Thinking About Objects: Identifying Objects’ States and Activities 2003 Prentice Hall, Inc All rights reserved 5.1 Introduction • Continue structured-programming discussion – Introduce Java’s remaining control structures 2003 Prentice Hall, Inc All rights reserved 5.2 Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires: – – – – Control variable (loop counter) Initial value of the control variable Increment/decrement of control variable through each loop Condition that tests for the final value of the control variable 2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Outline // Fig 5.1: WhileCounter.java // Counter-controlled repetition import java.awt.Graphics; import javax.swing.JApplet; Control-variableCondition name is counter tests for counter’s final value Control-variable initial value is public class WhileCounter extends JApplet { // draw lines on applet’s background public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from JApplet int counter = 1; WhileCounter.ja va Line 14 Line 16 Increment Line for counter 18 // initialization while ( counter