|
Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage |
Introduction to Programming - (CPTR125) - Lectures
JAVA - Lecture 5 - Version 2.1
Control Structures Part: 2
Lecture Outline
- Essentials of Counter-Controlled Repetition
- for Repetition Statement
- Examples Using the for Statement
- do…while Repetition Statement
- break and continue Statements
- Labeled break and continue Statements
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
for Repetition Statement
for statement header components
for ( initialization; loopContinuationCondition; increment )
statement;
can usually be rewritten as:
initialization;
while (loopContinuationCondition){
statement;
increment;
}
1 // Fig. 5.1: ForCounter.java
2 // Counter-controlled repetition with the for statement.
3 import java.awt.Graphics;
4 import javax.swing.JApplet;
6 public class ForCounter extends JApplet {
8
9 // draw lines on applet’s background
10 public void paint( Graphics g )
11 {
12 super.paint( g ); // call paint method inherited from JApplet
13
14 // for statement header includes initialization,
15 // repetition condition and increment
16 for ( int counter = 1; counter <= 10; counter++ )
17 g.drawLine( 10, 10, 250, counter * 10 );
18
19 } // end method paint
20
21 } // end class ForCounter
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 )
1 // Fig. 5.2: Sum.java
2 // 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 sum
10
11 // total even integers from 2 through 100
12 for ( int number = 2; number <= 100; number += 2 )
13 total += number; // end of for loop
14
15 // display results
16 JOptionPane.showMessageDialog( null, "The sum is " + total,
17 "Total Even Integers from 2 to 100", JOptionPane.INFORMATION_MESSAGE );
18
19
20 System.exit( 0 ); // terminate application
21
22 } // end main
23
24 } // end class Sum
do…while Repetition Statement- do…while structure
- Similar to while structure
- Tests loop-continuation after performing body of loop
- i.e., loop body always executes at least once1 // Fig. 5.3: DoWhileTest.java
2 // Using the do...while statement.
3 import java.awt.Graphics;
4
5 import javax.swing.JApplet;
6
7 public class DoWhileTest extends JApplet {
8
9 // draw lines on applet
10 public void paint( Graphics g )
11 {
12 super.paint( g ); // call paint method inherited from JApplet
13
14 int counter = 1; // initialize counter
15
16 do {
17 g.drawOval( 110 - counter * 10, 110 - counter * 10, counter * 20, counter * 20 );
18 ++counter;
19
20 } while ( counter <= 10 ); // end do...while
21
22 } // end method paint
23
24 } // end class DoWhileTest
break and continue Statements• break/continue
– Alter flow of control
• break statement
– Causes immediate exit from control structure
• Used in while, for, do…while or switch statements
• continue statement
– Skips remaining statements in loop body
– Proceeds to next iteration
• Used in while, for or do…while statementsLabeled break and continue Statements
• Labeled block
– Set of statements enclosed by {}
– Preceded by a label
• Labeled break statement
– Exit from nested control structures
– Proceeds to end of specified labeled block
1 // Fig. 5.13: BreakLabelTest.java
2 // Labeled break statement.
3 import javax.swing.JOptionPane;
4
5 public class BreakLabelTest {
6
7 public static void main( String args[] )
8 {
9 String output = "";
10
11 stop: { // labeled block
12
13 // count 10 rows
14 for ( int row = 1; row <= 10; row++ ) {
15
16 // count 5 columns
17 for ( int column = 1; column <= 5 ; column++ ) {
18
19 if ( row == 5 ) // if row is 5,
20 break stop; // jump to end of stop block
21
22 output += "* ";
23
24 } // end inner for
25
26 output += "\n";
27
28 } // end outer for
29
30 // following line is skipped
31 output += "\nLoops terminated normally";
32
33 } // end labeled block
34
35 JOptionPane.showMessageDialog( null, output, "Testing break with a label",
36 JOptionPane.INFORMATION_MESSAGE );
37
38
39 System.exit( 0 ); // terminate application
40
41 } // end main
42
43 } // end class BreakLabelTest
Source: Java How to Program – 5th Edition – Deitel & Deitel
Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage |