Chapter 5 - Control Structures - Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 The for Repetition Structure 5.4 Examples Using the for Structure 5.5 The switch Multiple-Selection Structure 5.6 The do/while Repetition Structure 5.7 The break and continue Statements 5.8 The Labeled break and continue Statements 5.9 Logical Operators 5.10 Structured Programming Summary
Chapter 5 - Control Structures - Part 2. Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition 5.3The for Repetition Structure 5.4Examples Using the for Structure 5.5The switch Multiple-Selection Structure 5.6The do / while Repetition Structure - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 5 - Control Structures - Part 2
Outline5.1 Introduction5.2 Essentials of Counter-Controlled Repetition5.3 The for Repetition Structure5.4 Examples Using the for Structure5.5 The switch Multiple-Selection Structure5.6 The do/while Repetition Structure5.7 The break and continue Statements5.8 The Labeled break and continue Statements5.9 Logical Operators5.10 Structured Programming Summary
15 outputTextArea.append( "Year\tAmount on deposit\n" );
21 }
17 for ( int year = 1; year <= 10; year++ ) {18 amount = principal * Math.pow( 1.0 + rate, year );19 outputTextArea.append( year + "\t" +20 precisionTwo.format( amount ) + "\n" );
5.4 Examples Using the for Structure
– static method showMessageDialog • Class JOptionPane• Up till now, displayed Strings• showMessageDialog can display a String or GUI
component, such as a JTextArea
23 JOptionPane.showMessageDialog(
24 null, outputTextArea, "Compound Interest",
25 JOptionPane.INFORMATION_MESSAGE );
1. import
2. Class Interest
2.1 Initialize variables
2.2 DecimalFormat
3. for loop
3.1 Math.pow
3.2 append
3.3 format
3.4 showMessageDialog
1// Fig. 5.6: Interest.java
2// Calculating compound interest
33import java.text.DecimalFormat;
4import javax.swing.JOptionPane;
5import javax.swing.JTextArea;
6
7public class Interest {
8 public static void main( String args[] )
9 {
10 double amount, principal = 1000.0, rate = .05;
11
12 DecimalFormat precisionTwo = new DecimalFormat( "0.00" );
1313 JTextArea outputTextArea = new JTextArea( 11, 20 );
14
15 outputTextArea.append( "Year\tAmount on deposit\n" );
16
17 for ( int year = 1; year <= 10; year++ ) {
1818 amount = principal * Math.pow( 1.0 + rate, year );
1919 outputTextArea.append( year + "\t" +
2020 precisionTwo.format( amount ) + "\n" );
21 }
22
2323 JOptionPane.showMessageDialog(
24 null, outputTextArea, "Compound Interest",
25 JOptionPane.INFORMATION_MESSAGE );
26
27 System.exit( 0 ); // terminate the application
28 }
29}
Notice the import statements required.
New JTextArea object initialized to hold 11 rows and 20 columns of text.
new operator used to create new objects.
Use method append to add to the String in the JTextArea object.
Notice the format of method Math.pow
Use method format to output the formatted number as a String.
Use the JTextArea reference as an argument to showMessageDialog
Program Output
5.5 The switch Multiple-Selection Structure
• switch statements– Useful to test a variable for different values
• Different action taken
• Format– Series of case labels and an optional default caseswitch ( value ){
case '1':actions
case '2':actions
default:actions
}– break; causes exit from structure
5.5 The switch Multiple-Selection Structure
true
false
.
.
.
case a case a action(s) break
case b case b action(s) break
false
false
case z case z action(s) break
true
true
default action(s)
Class SwitchTest
1// Fig. 5.7: SwitchTest.java2// Counting letter grades3import java.awt.Graphics;4import javax.swing.*;56public class SwitchTest extends JApplet {7 int choice; 89 public void init()10 {11 String input;1213 input = JOptionPane.showInputDialog( 14 "Enter 1 to draw lines\n" +15 "Enter 2 to draw rectangles\n" +16 "Enter 3 to draw ovals\n" );1718 choice = Integer.parseInt( input );19 }2021 public void paint( Graphics g )22 {23 for ( int i = 0; i < 10; i++ ) { 24 switch( choice ) {25 case 1:26 g.drawLine( 10, 10, 250, 10 + i * 10 );27 break;28 case 2:29 g.drawRect( 10 + i * 10, 10 + i * 10,30 50 + i * 10, 50 + i * 10 );31 break;
Program Output
34 50 + i * 10, 50 + i * 10 );
35 break;
36 default:
37 JOptionPane.showMessageDialog(
38 null, "Invalid value entered" );
39 } // end switch
40 } // end for
41 } // end paint()
42} // end class SwitchTest
32 case 3:33 g.drawOval( 10 + i * 10, 10 + i * 10,
• break– Immediate exit from while, for, do/while or switch– Program continues with the first statement after the structure
– Common uses of the break statement• Escape early from a loop
• Skip the remainder of a switch structure
5.7 The break and continue Statements
• continue– Skips the remaining statements in body of while, for or do/while
• Proceeds with the next iteration of the loop
– while and do/while• Loop-continuation test is evaluated immediately after continue
– for structure• Increment expression is executed, then the loop-continuation
test is evaluated
1. Class BreakTest
2. main
2.1 for loop
2.2 break
Program Output
1// Fig. 5.11: BreakTest.java2// Using the break statement in a for structure3import javax.swing.JOptionPane;45public class BreakTest {6 public static void main( String args[] )7 {8 String output = "";9 int count;1011 for ( count = 1; count <= 10; count++ ) {12 if ( count == 5 )
1313 break; // break loop only if count == 51415 output += count + " ";16 }1718 output += "\nBroke out of loop at count = " + count;19 JOptionPane.showMessageDialog( null, output );20 System.exit( 0 );21 }22}
break causes an immediate exit from the loop.
1. Class ContinueTest
2. main
2.1 for loop
2.2 continue
Program Output
1// Fig. 5.12: ContinueTest.java2// Using the continue statement in a for structure3import javax.swing.JOptionPane;45public class ContinueTest {6 public static void main( String args[] )7 {8 String output = "";910 for ( int count = 1; count <= 10; count++ ) {11 if ( count == 5 )
1212 continue; // skip remaining code in loop13 // only if count == 51415 output += count + " ";16 }1718 output += "\nUsed continue to skip printing 5";19 JOptionPane.showMessageDialog( null, output );20 System.exit( 0 );21 }
continue skips the rest of the body and goes to the next iteration.
5.8 The Labeled break and continue Statements
• Nested set of structures– break statement
• Can only break out of immediately enclosing structure
– Use labeled break statement• Label - identifier followed by colon, i.e. myLabel:• Breaks out of enclosing statement and any number of
repetition structures
• Program resumes after enclosing labeled compound statement
– Labeled continue statement• Skips statements in enclosing structure
• Continues with next iteration of enclosing labeled repetition structure
– Repetition structure preceded by a label
1. Class BreakLabelTest
2. stop:
2.1 for loop
2.2 Nested for loop
2.3 break stop
1// Fig. 5.13: BreakLabelTest.java
2// Using the break statement with a label
3import javax.swing.JOptionPane;
4
5public class BreakLabelTest {
6 public static void main( String args[] )
7 {
8 String output = "";
9
1010 stop: { // labeled compound statement
11 for ( int row = 1; row <= 10; row++ ) {
12 for ( int column = 1; column <= 5 ; column++ ) {