Chapter 5 – Control Structures: Part 25.1 Introduction5.2 Essentials of Counter-Controlled RepetitionWhileCounter.java Line 14 Line 16 Line 185.3 for Repetition StatementForCounter.java Line 16 int counter = 1; Line 16 counter <= 10; Line 16 counter++;PowerPoint Presentation5.3 for Repetition Structure (cont.)Slide 95.4 Examples Using the for StatementSum.java Line 12Interest.java Lines 13-15 Line 18 Line 19Interest.java Lines 28-315.5 do…while Repetition StatementDoWhileTest.java Lines 16-20Slide 165.6 switch Multiple-Selection StatementSwitchTest.java Lines 16-21: Getting user’s inputSwitchTest.java Line 32: controlling expression Line 32: switch statement Line 48SwitchTest.javaSlide 21Slide 225.7 break and continue StatementsBreakTest.java Line 12 Lines 14-15ContinueTest.java Line 11 Lines 13-145.8 Labeled break and continue StatementsBreakLabelTest.java Line 11 Line 14 Line 17 Lines 19-20BreakLabelTest.javaContinueLabelTest.java Line 11 Line 14 Line 17 Lines 21-22ContinueLabelTest.java5.9 Logical OperatorsSlide 32Slide 33LogicalOperators.java Lines 16-20 Lines 23-27LogicalOperators.java Lines 30-34 Lines 37-41 Lines 44-48 Lines 51-53LogicalOperators.javaSlide 375.10 Structured Programming SummarySlide 39Slide 40Slide 41Slide 42Slide 435.11 (Optional Case Study) Thinking About Objects: Identifying Objects’ States and ActivitiesSlide 455.11 (Optional Case Study) Thinking About Objects: Identifying Objects’ States and Activities (cont.):Slide 47Slide 48 2003 Prentice Hall, Inc. All rights reserved.1Chapter 5 – Control Structures: Part 2Outline5.1 Introduction5.2 Essentials of Counter-Controlled Repetition5.3 for Repetition Statement5.4 Examples Using the for Statement5.5 do…while Repetition Statement5.6 switch Multiple-Selection Statement5.7 break and continue Statements5.8 Labeled break and continue Statements5.9 Logical Operators5.10 Structured Programming Summary5.11 (Optional Case Study) Thinking About Objects: Identifying Objects’ States and Activities 2003 Prentice Hall, Inc. All rights reserved.25.1 Introduction•Continue structured-programming discussion–Introduce Java’s remaining control structures 2003 Prentice Hall, Inc. All rights reserved.35.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.Outline4WhileCounter.javaLine 14Line 16Line 181 // Fig. 5.1: WhileCounter.java2 // Counter-controlled repetition.3 import java.awt.Graphics;4 5 import javax.swing.JApplet;6 7 public class WhileCounter extends JApplet {8 9 // draw lines on applet’s background10 public void paint( Graphics g ) 11 { 12 super.paint( g ); // call paint method inherited from JApplet13 14 int counter = 1; // initialization15 16 while ( counter <= 10 ) { // repetition condition17 g.drawLine( 10, 10, 250, counter * 10 );18 ++counter; // increment19 20 } // end while21 22 } // end method paint23 24 } // end class WhileCounterIncrement for counterCondition tests for counter’s final value Control-variable name is counterControl-variable initial value is 1 2003 Prentice Hall, Inc. All rights reserved.55.3 for Repetition Statement•Handles counter-controlled-repetition details 2003 Prentice Hall, Inc.All rights reserved.Outline6ForCounter.javaLine 16int counter = 1;Line 16 counter <= 10;Line 16 counter++;1 // Fig. 5.2: ForCounter.java2 // Counter-controlled repetition with the for statement.3 import java.awt.Graphics;4 5 import javax.swing.JApplet;6 7 public class ForCounter extends JApplet {8 9 // draw lines on applet’s background10 public void paint( Graphics g )11 {12 super.paint( g ); // call paint method inherited from JApplet13 14 // for statement header includes initialization, 15 // repetition condition and increment16 for ( int counter = 1; counter <= 10; counter++ ) 17 g.drawLine( 10, 10, 250, counter * 10 );18 19 } // end method paint20 21 } // end class ForCounterCondition tests for counter’s final value Control-variable name is counterControl-variable initial value is 1Increment for counter 2003 Prentice Hall, Inc. All rights reserved.7Fig. 5.3 for statement header components. for ( int counter = 1; counter <= 10; counter++ )Increment of control variableControl variableFinal value of control variable for which the condition is truefor keywordLoop-continuation conditionInitial value of control variableRequired semicolon separatorRequired semicolon separator 2003 Prentice Hall, Inc. All rights reserved.85.3 for Repetition Structure (cont.)for ( initialization; loopContinuationCondition; increment ) statement;can usually be rewritten as:initialization;while ( loopContinuationCondition ) { statement; increment;} 2003 Prentice Hall, Inc. All rights reserved.9Fig. 5.4 for statement activity diagram. [counter <= 10][counter > 10]int counter = 1counter++Determine whether the final value of control variable has been reachedg.drawLine( 10, 10, 250, counter * 10 );Establish initial value of control variableDraw a line on the appletIncrement the control variable 2003 Prentice Hall, Inc. All rights reserved.105.4 Examples Using the for Statement•Varying control variable in for statement–Vary control variable from 1 to 100 in increments of 1•for ( int i = 1; i <= 100; i++ )–Vary control variable from 100 to 1 in increments of –1•for ( int i = 100; i >= 1; i-- )–Vary control variable from 7 to 77 in increments of 7•for ( int i = 7; i <= 77; i += 7 ) 2003 Prentice Hall, Inc.All rights reserved.Outline11Sum.javaLine 121 // Fig. 5.5: Sum.java2 // Summing integers with the for statement.3 import javax.swing.JOptionPane;4 5 public class Sum {6 7 public static void main( String args[] )8 {9 int total = 0; // initialize sum10 11 // total even integers from 2 through 10012 for ( int number = 2; number <= 100; number += 2 )13 total += number; 14 15 // display results16
View Full Document